std::vector works with classes that are not default constructible?

前端 未结 4 735
醉话见心
醉话见心 2021-02-14 01:36

I\'ve read in several places that std::vector requires it\'s template argument to be default constructible. Today I just tried it with one of my classes that has a delete<

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-14 01:55

    Well, templates are in some meaning weakly typed. That is, the missing default constructor won't be detected until your code calls the method where it's used, perhaps internally -- this will give a compile-time error.

    However, unless you are not touching the methods which use the default constructor internally, you are "safe". However, I don't know which is the "safe" subset, and I suspect that it's not defined by the standard. Example: vector copying might use resize, which in turn might use default constructor.

提交回复
热议问题