type requirements for std::vector

前端 未结 5 1473
夕颜
夕颜 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:39

    The C++11 standard does indeed require CopyInsertable as others have pointed out. However this is a bug in the C++11 standard. This has since been corrected in N3376 to MoveInsertable and DefaultInsertable.

    The vector::resize(n) member function requires MoveInsertable and DefaultInsertable. These roughly translate to DefaultConstructible and MoveConstructible when the allocator A uses the default construct definitions.

    The following program compiles using clang/libc++:

    #include 
    #include 
    
    struct A {
      A() : X(0) { std::cerr<<" A::A(); this="< a;
      a.resize(4);
    }
    

    and for me prints out:

     A::A(); this=0x7fcd634000e0
     A::A(); this=0x7fcd634000e4
     A::A(); this=0x7fcd634000e8
     A::A(); this=0x7fcd634000ec
    

    If you remove the move constructor above and replace it with a deleted copy constructor, A is no longer MoveInsertable/MoveConstructible as move construction then attempts to use the deleted copy constructor, as correctly demonstrated in the OP's question.

提交回复
热议问题