enable_shared_from_这不适用于xcode 5

ali*_*glu 10 c++ xcode llvm clang c++11

#include <iostream>
#include <memory>

template<typename T>
class Test: public std::enable_shared_from_this< Test<T> >
{

public:

    std::shared_ptr< Test<T> > getMe()
    {
        return shared_from_this();
    };

};

int main(int argc, const char * argv[])
{
    Test<int>   aTest;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在Xcode 5上编译时,我得到了

Use of undeclared identifier 'shared_from_this'
Run Code Online (Sandbox Code Playgroud)

我测试了它并在Visual Studio 2010上工作.

How*_*ant 16

    return this->shared_from_this();
           ^^^^^^
Run Code Online (Sandbox Code Playgroud)

VC++ 2010没有完全正确地实现模板化基类的查找规则.铿锵行为是正确的.上述修复程序将使其适用于您的两个平台.

  • @ali_nakipoglu:嗯,总有标准...特别是14.6.2/3*在类或类模板的定义中,如果基类依赖于模板参数,则不会检查基类范围在类模板或成员的定义点或类模板或成员的实例化期间,无限制的名称查找.*但您也可以谷歌进行两阶段查找. (2认同)