在 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/