gpt4 book ai didi

c++ - 在 clang++ 上使用带有 enable_shared_from_this 的 shared_ptr 时编译错误

转载 作者:行者123 更新时间:2023-11-30 05:45:55 25 4
gpt4 key购买 nike

考虑这段代码

#include <memory>

class FooBar : public std::enable_shared_from_this<FooBar>{};
typedef std::shared_ptr<const FooBar> ConstantPointer;

int main()
{
ConstantPointer p(new FooBar());
return 0;
}

在 g++ 版本 4.7.4 上,使用 -std=c++11 选项编译没有问题

在 clang++ 版本上Apple LLVM 版本 6.0 (clang-600.0.57)(基于 LLVM 3.5svn)

它产生以下编译错误

In file included from main.cpp:1:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4013:35: error: no viable overloaded '='
__e->__weak_this_ = *this;
~~~~~~~~~~~~~~~~~ ^ ~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4050:5: note: in instantiation of function template specialization
'std::__1::shared_ptr<const FooBar>::__enable_weak_this<FooBar>' requested here
__enable_weak_this(__p);
^
main.cpp:8:20: note: in instantiation of function template specialization 'std::__1::shared_ptr<const FooBar>::shared_ptr<FooBar, void>' requested here
ConstantPointer p(new FooBar());
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4942:15: note: candidate function not viable: no known conversion from
'std::__1::shared_ptr<const FooBar>' to 'const std::__1::weak_ptr<FooBar>' for 1st argument
weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4949:9: note: candidate template ignored: could not match 'weak_ptr' against 'shared_ptr'
operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4967:13: note: candidate template ignored: disabled by 'enable_if' [with _Yp = const FooBar]
is_convertible<_Yp*, element_type*>::value,

我的问题是它是编译器错误还是非确认代码?

最佳答案

首先,clang 3.5.0(不是 Apple 的)编译了您的示例,因此您的特定版本或安装的 clang 似乎有问题。

其次,我相信您的示例格式正确且定义明确,因为标准没有指定拥有 shared_ptr 的确切类型参数。实例(在您的示例中为 p):

[util.smartptr.enab]/7

shared_ptr<T> shared_from_this();
shared_ptr<T const> shared_from_this() const;

Requires: enable_shared_from_this<T> shall be an accessible base class of T. *this shall be a subobject of an object t of type T. There shall be at least one shared_ptr instance p that owns &t.

关于c++ - 在 clang++ 上使用带有 enable_shared_from_this 的 shared_ptr 时编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29182930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com