Using std::shared_ptr with clang++ and libstdc++

后端 未结 2 2036
伪装坚强ぢ
伪装坚强ぢ 2021-01-04 09:47

I\'m trying to use the std::shared_ptr in clang++(clang version 3.1 (trunk 143100)) using libstdc++(4.6.1). I have a little demo program:

#include 

        
2条回答
  •  猫巷女王i
    2021-01-04 10:18

    The implicitly-declared copy constructor for shared_ptr is deleted because shared_ptr has a move constructor or a move assignment operator (or both), per C++11 12.8p7:

    If the class definition does not explicitly declare a copy constructor, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted (8.4).

    GCC 4.6.x does not implement this rule, which came into the C++11 working paper very late in the process as N3203=10-0193. The shared_ptr in libstdc++ 4.6.x was correct at the time it was written, but C++11 changed after that.. Boost had exactly the same issue with it's shared_ptr, and this is one of the common incompatibilities between GCC and Clang.

    Adding a defaulted copy constructor and copy assignment operator to shared_ptr will fix the problem.

提交回复
热议问题