acceptable fix for majority of signed/unsigned warnings?

后端 未结 7 734
清酒与你
清酒与你 2021-01-12 03:32

I myself am convinced that in a project I\'m working on signed integers are the best choice in the majority of cases, even though the value contained within can never be neg

7条回答
  •  天命终不由人
    2021-01-12 04:01

    Don't derive publicly from STL containers. They have nonvirtual destructors which invokes undefined behaviour if anyone deletes one of your objects through a pointer-to base. If you must derive e.g. from a vector, do it privately and expose the parts you need to expose with using declarations.

    Here, I'd just use a size_t as the loop variable. It's simple and readable. The poster who commented that using an int index exposes you as a n00b is correct. However, using an iterator to loop over a vector exposes you as a slightly more experienced n00b - one who doesn't realize that the subscript operator for vector is constant time. (vector::size_type is accurate, but needlessly verbose IMO).

提交回复
热议问题