【问题标题】:Compilation errors with std::enable_shared_from_this<> on OS X在 OS X 上使用 std::enable_shared_from_this<> 的编译错误
【发布时间】:2014-03-15 09:46:48
【问题描述】:

一位用户来找我在 OS X 上遇到编译问题:

http://fpaste.org/77628/39251593/

排除明显后;使用 gcc 而不是 clang,以及“正确的”标准库,很明显问题出在类声明的 std::enable_shared_from_this 部分

类本身有一个声明

class Expression : public std::enable_shared_from_this<Expression> {
// ...
};

一些调查表明这可能是 Xcode/clang 本身的问题: https://github.com/kripken/emscripten/issues/270

https://code.google.com/p/alembic/issues/detail?id=315

有人可以确认吗?或者,有没有办法解决这个问题?

代码位于here,相关文件位于here

编辑:有问题的代码是不需要的,所以被删除了。使用特定提交更新链接 (r1047)

【问题讨论】:

    标签: c++ xcode inheritance c++11 shared-ptr


    【解决方案1】:

    我相信这是我遇到的 libc++ 中的一个错误。我不久前提交了一个错误报告 (http://llvm.org/bugs/show_bug.cgi?id=18843)。

    问题的出现是因为从std::enable_shared_from_this 派生的类的某些const 版本的shared_ptr 被分配了一个指向该类型的非常量指针。在 libstdc++ 中,这个 const 在对 std::enable_shared_from_this 中的内部 weak_ptr 进行赋值的函数中被内部剥离,但这在 libc++ 中不会发生。以下代码显示了在当前 libc++ 下会导致此错误的示例:

    struct A : std::enable_shared_from_this<A> {};
    
    int main()
    {
      std::shared_ptr<A const> ptr( new A() );
    }
    

    您现在可以通过以下方式解决此问题:

    std::shared_ptr<A const> ptr( std::const_pointer_cast<A const>( std::shared_ptr<A>( new A() ) ) );
    

    new A() 只是您试图进入shared_ptr 的指针。

    【讨论】:

    • 似乎是这样。为证实我的怀疑而欢呼
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 2015-09-25
    相关资源
    最近更新 更多