以下代码无法在Visual C 2008和2010上编译:
- #include <memory>
- struct A {};
- std::auto_ptr<A> foo() { return std::auto_ptr<A>(new A); }
- const std::auto_ptr<A> bar() { return std::auto_ptr<A>(new A); }
- int main()
- {
- const std::auto_ptr<A> & a = foo(); // most important const
- const std::auto_ptr<A> & b = bar(); // error C2558:
- // class 'std::auto_ptr<_Ty>' :
- // no copy constructor available or copy
- // constructor is declared 'explicit'
- bar(); // No error?
- }
我期望“最重要的const”应用于变量“b”,然而,它不会编译,并且由于某种原因,编译器要求复制构造函数(这让我感到惊讶,因为这里不应该涉及复制) .对bar()的独立调用工作正常,这意味着,我猜,实际上b的初始化是问题所在.
(也许它在C 98中被禁止并在C 11中被授权?)
注意:它在Visual C 2012,gcc 4.6和Solaris CC(所有编译器……)上编译,但不是gcc 3.4,也不是XL C)