【发布时间】:2020-12-28 23:43:19
【问题描述】:
在一段时间后(我已经更新了 macOS 和 Xcode)之后,我不再能够从 Xcode 或通过在命令行上调用 clang 来构建我的 C++ 项目。在我的代码中每次使用 std::shared_ptr::make_shared 都会出错。我想强调 这用于编译 并且运行良好,但我不明白发生了什么变化。也许更新 xcode 以某种方式破坏了我的工具链。
我在一些非常简单的代码中重现了这个问题,我认为这些代码应该可以正常编译:
#include <string>
#include <memory>
class test_c {
private:
std::string str_;
public:
test_c(std::string str__) : str_(str__) { }
};
std::string test_s = "test";
auto test = std::shared_ptr<test_c>::make_shared(test_s);
错误如下所示:
$ clang -std=c++14 test.cpp
test.cpp:10:38: error: no member named 'make_shared' in 'std::__1::shared_ptr<test_c>'
auto test = std::shared_ptr<test_c>::make_shared(test_s);
~~~~~~~~~~~~~~~~~~~~~~~~~^
1 error generated.
【问题讨论】:
-
AFAIK,
std::shared_ptr从来没有一个名为make_shared的静态成员函数。auto test = std::make_shared(test_s);应该为你编译。 -
此代码编译成功时您使用了哪些编译器/版本/构建设置?
-
你不能用私有构造函数编译:D
-
@NathanOliver 我相信这是一个较旧的 Xcode,我认为是 10.3。我记得我已经更改了构建设置以专门使用 C++14 方言,但是这些选项似乎都不再起作用了。我什至不能在命令行上构建。
-
@NathanOliver 文档似乎同意你的观点(不知道我之前是如何编译的)但现在我得到“候选模板被忽略:无法推断模板参数”......