c++ - Clang 4.6.2,使用 shared_ptr

标签 c++ clang shared-ptr google-nativeclient

在 PNaCl 中,我使用 libstdc++ 来编译相同的代码。但是我想使用 shared_ptr 并且我得到了这个错误:

error: call to implicitly-deleted copy constructor of

这个问题有问题:

Using std::shared_ptr with clang++ and libstdc++

我明白了,但我不知道如何解决这个问题。我引用解决方案:

Adding a defaulted copy constructor and copy assignment operator to shared_ptr will fix the problem.

我在 shared_ptr.h 中添加了这个:

shared_ptr(const shared_ptr&) noexcept = default;

但是编译器现在返回这个错误:

F:/nacl_sdk/pepper_31/toolchain/win_pnacl/usr/include/c++/4.6.2/bits/shared_ptr.
h:268:19: error:
  exception specification of explicitly defaulted copy assignment operator
  does not match the calculated one
  shared_ptr& operator=(const shared_ptr&) noexcept = default;
              ^

在这连串的错误中,我到此为止。

有人有提示吗?

最佳答案

我能够用这个简单的测试用例重现你的错误:

#include <memory>

int main() {
  std::shared_ptr<int> foo;
  std::shared_ptr<int> bar(foo);
  std::shared_ptr<int> baz;
  baz = foo;
  return 0;
}

使用 pnacl-clang++ -std=c++11 -stdlib=libstdc++ test.cc 构建

修复它的最简单方法是使用 libc++ 而不是 libstdc++:

pnacl-clang++ -std=c++11 -stdlib=libc++ test.cc

这在 pepper_31 中是“实验性的”,但在 pepper_33 和更高版本(目前是 pepper_canary)中将是默认设置。参见 https://groups.google.com/forum/#!topic/native-client-discuss/0spfg6O04FM有关此开关的更多信息。

我还能够破解两个 header ,使其与 libstdc++ 一起工作:

在 bits/shared_ptr.h 中:

template<typename _Tp>
class shared_ptr : public __shared_ptr<_Tp>
{
public:
  shared_ptr(const shared_ptr&) = default;
  shared_ptr& operator =(const shared_ptr&) = default;
  ...

在 bits/shared_ptr_base.h 中:

template<typename _Tp, _Lock_policy _Lp>
class __shared_ptr
{
public:
  __shared_ptr(const __shared_ptr&) = default;
  __shared_ptr& operator =(const __shared_ptr&) = default;
  ...

IMO,在这里使用 libc++ 是更好的解决方案。 :)

关于c++ - Clang 4.6.2,使用 shared_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20502010/

相关文章:

clang - Windows 上的 lldb,可能吗?

vim - lang完成不起作用

c++ - 限制 boost make_shared 采用的参数

c++ - 非类型模板参数的类型转换无效

c++ - 在 Visual C++ DLL 项目中包含 ntdsapi.h 和 activeds.h 时出错

c++ - boost::async_read 中的 EOF 与 thread_pull 和 boost 1.54

clang - clang 的包含优先级是怎么回事?

c++ - 在派生类的对象创建期间使用 shared_from_this() 的两阶段构造

C++11:如果对象是使用 make_shared 构造的,如何删除它

c++ - 创建一个没有指针但有索引的链表