在clang++上使用带有enable_shared_from_this的shared_ptr时编译错误
Compile errors while using shared_ptr with enable_shared_from_this on clang++
考虑这段代码
#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(不是苹果的)编译了您的示例,所以您的clang的特定版本或安装似乎有问题。
其次,我相信您的示例格式良好,定义良好,因为标准没有指定所属shared_ptr
实例(示例中为p
)的确切类型参数:
[util.smartptr.enab]/7
shared_ptr<T> shared_from_this(); shared_ptr<T const> shared_from_this() const;
要求:
enable_shared_from_this<T>
应为T
的可访问基类。*this
应当是类型为T
的对象t
的子对象至少有一个shared_ptr
实例p
拥有&t
相关文章:
- 表示"accepting anything for this template argument" C++概念的通配符
- 为什么使用 "this" 指针调用派生成员函数?
- C++错误:"error: int aaa::bbb is protected within this context"
- 我可以将调用类的"this"传递给 lambda 函数吗?
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 关于C++中具有多重继承"this"指针的说明
- 在noexcept 规范中是否允许使用"this"?
- 如何修复"error: ‘_1’ was not declared in this scope"?
- C++调用具有 *this 属性的单个帮助程序函数
- Doees the 'this' 指针参与虚函数的多态行为
- 在 c++ 中正确定义"this"关键字?
- 在什么情况下,两个堆栈分配的结构对象的 this 点指向同一个地址?
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- QObject::连接无法将信号连接到*this*对象的插槽
- 析构函数中的"delete this"
- 为什么成员函数内的"this"指针为空?
- 为什么我需要在转换构造函数上引用 this->?
- "this"指针的值在对象的生存期内是否恒定?
- 引用 std::shared:ptr 以避免引用计数
- C++ "Invalid use of 'this' in non-member function" ,