Class containing auto_ptr stored in vector

前端 未结 5 954
小蘑菇
小蘑菇 2021-01-21 05:42

In an answer to Is it safe to store objects of a class which has an std::auto_ptr as its member variable in std::vector? I stated that a class that contained an auto_ptr could

5条回答
  •  日久生厌
    2021-01-21 06:21

    I don't think it's necessarily the case that the above code will even compile. Surely the implementor of std::vector is at liberty to require an assignment operator to be available, from const A&?

    And having just tried it, it doesn't compile on Visual Studio C++ 2008 Service Pack 1:

    binary '=' : no operator found which takes a right-hand operand of type 'const A' (or there is no acceptable conversion)

    My guess is that, on the guidance of Herb Sutter, the container classes in VC++ make every effort to impose the standard requirements on their type parameters, specifically to make it hard to use auto_ptr with them. They may have overstepped the boundaries set by the standard of course, but I seem to remember it mandating true assignment as well as true copy construction.

    It does compile in g++ 3.4.5, however.

提交回复
热议问题