type requirements for std::vector

前端 未结 5 1485
夕颜
夕颜 2020-12-21 17:17

I am still confused about the requirements for a type to be used with a std::vector in C++11, but this may be caused by a buggy compiler (gcc 4.7.0). This code:

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-21 17:36

    To use a class in a vector it should have a copy constructor/assignment operator or a noexcept move constructor/assignment operator. GCC is quite correct not to compile your example which doesn't have any of these.

    How would you except the vector to do anything without being able to copy or move what it contains?

    The reason that the first example works is that since you didn't define any copy or move constructors or assignment operators, you get the defaults. In the second example, since you explicitly deleted the copy constructor, you don't get any automatically generated constructors or assignment operators.

提交回复
热议问题