2

每当我尝试使用带有任何参数的构造函数时,我都会收到编译器错误“没有匹配的函数来调用'make_shared'”。因此,例如:

std::shared_ptr<int> foo = std::make_shared<int> ();

工作正常。但,

std::shared_ptr<int> foo = std::make_shared<int> (10);

给出以下错误:

/usr/bin/clang  -g -Wall -Wextra -Wc++11-extensions -c ./test.cpp
./test.cpp:7:30: error: no matching function for call to 'make_shared'
  std::shared_ptr<int> foo = std::make_shared<int> (10);
                         ^~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4700:1: note: candidate function [with _Tp = int, _A0 = int]
  not viable: expects an l-value for 1st argument
make_shared(_A0& __a0)

我直接从这里获取了上面的代码http://www.cplusplus.com/reference/memory/make_shared/ 并且该代码在 cpp.sh 网站上运行良好。我怀疑我的编译器设置有问题。在 Macbook 上的 iTerm 中运行。此外,即使我删除了上面显示的各种 clang 选项,我也会遇到同样的错误。有任何想法吗?我的头文件是否可能需要更新?它是从 2015 年 9 月 4 日开始的。似乎最近足以让 C++11 工作。

$ /usr/bin/clang --version
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin17.7.0
Thread model: posix

$ ls -l /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory
-rw-r--r--  1 root  wheel  174919 Sep  4  2015 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory
4

1 回答 1

2

错误消息抱怨您使用 prvalue 10。尝试使用

int avar = 10;
auto foo = std::make_shared<int> (avar);

看看使用左值时会发生什么很有趣。

您是否在本地构建了 std 库?如果你是,也许你可以尝试再次重建或从某个地方获取预建库。

我在https://godbolt.org/上使用配置x86-64 clang 7.0.0-std=c++11. 它工作正常。即使你使用 iOS,我猜它在那个操作系统上也应该不错。

我也看到你-Wc++11-extensions在建造时使用。试试改用-std=c++11吧?


DG编辑:正如我在下面的评论中所指出的,最后一个建议“尝试使用-std=c++11”奏效了! 然后所有值(-std=c++11左值、右值、纯右值等)都可以正常工作。请参阅下面的评论。

于 2019-02-13T02:33:03.297 回答