【问题标题】:enable_shared_from_this not working on xcode 5enable_shared_from_this 不适用于 xcode 5
【发布时间】:2014-06-22 02:39:33
【问题描述】:
#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;
}

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

Use of undeclared identifier 'shared_from_this'

我在 Visual Studio 2010 上对其进行了测试。

【问题讨论】:

  • 你忘了用std:: 来限定它,就像std::shared_from_this()
  • 这里没有 std 命名空间,只需要从 enable_shared_from_this 扩展并返回 getMe 函数的类型,并且它们已经输入。
  • 加上 shared_from_this 是 enable_shared_from_this 类的成员函数。你确定你正在阅读这个问题吗?

标签: c++ xcode c++11 clang llvm


【解决方案1】:
    return this->shared_from_this();
           ^^^^^^

VC++ 2010 没有完全正确地实现模板化基类的查找规则。铿锵的行为是正确的。上述修复将使它在您的两个平台上都能正常工作。

【讨论】:

  • +1,以便更容易地通过 Google 搜索更多相关信息,这是查找 dependentnon-dependent 名称的问题。没有资格 shared_from_this不是从属名称。
  • 非常感谢!这就是诀窍。我仍然不明白为什么它在这里需要“这个”。有什么文档可以检查吗?
  • @ali_nakipoglu:嗯,总是有标准的……特别是 14.6.2/3 在类或类模板的定义中,如果基类依赖于模板-参数时,在类模板或成员的定义点或类模板或成员的实例化期间,在非限定名称查找期间不会检查基类范围。但您也可以在谷歌上进行两阶段查找。
  • 还有gcc.gnu.org/wiki/VerboseDiagnostics#dependent_base 以及那里给出的链接。
猜你喜欢
  • 1970-01-01
  • 2023-03-08
  • 2020-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
相关资源
最近更新 更多