【问题标题】:Xcode/clang no longer compiling C++14 code after updating Xcode: "no member named make_shared"Xcode/clang 更新 Xcode 后不再编译 C++14 代码:“没有名为 make_shared 的成员”
【发布时间】: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 文档似乎同意你的观点(不知道我之前是如何编译的)但现在我得到“候选模板被忽略:无法推断模板参数”......

标签: c++ xcode clang


【解决方案1】:

我根本找不到任何方法将make_shared 作为静态方法调用。还在旧版本的 Clang 中检查了您的代码。好像不行……

也许只是:

auto test = std::make_shared<test_c>(test_s);

UPD 等待。事实上,它在clang of version 9 中工作。看起来它真的被更新为禁用此功能... 不在v10 工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 2015-08-02
    相关资源
    最近更新 更多