【问题标题】:clang 3.1 can't see unique_ptr?clang 3.1 看不到 unique_ptr?
【发布时间】:2012-08-07 20:42:32
【问题描述】:

我刚刚开始玩 clang 并尝试编译以下示例程序:

#include <memory>
#include <iostream>

int main()
{
    std::unique_ptr<unsigned> u(new unsigned(10));
    std::cout << *u << std::endl;
    return 0;
}

编译时出现以下错误:

$ clang++ helloworld.cpp 
helloworld.cpp:6:10: error: no member named 'unique_ptr' in namespace 'std'
    std::unique_ptr<unsigned> u(new unsigned(10));
    ~~~~~^
helloworld.cpp:6:29: error: expected '(' for function-style cast or type construction
    std::unique_ptr<unsigned> u(new unsigned(10));
                    ~~~~~~~~^
helloworld.cpp:6:31: error: use of undeclared identifier 'u'
    std::unique_ptr<unsigned> u(new unsigned(10));
                              ^
helloworld.cpp:7:19: error: use of undeclared identifier 'u'
    std::cout << *u << std::endl;
                  ^
4 errors generated.

我在 Mac OS X 上使用 Clang 3.1:

$ clang++ --version
Apple clang version 3.1 (tags/Apple/clang-318.0.61) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.4.0
Thread model: posix

任何想法为什么这不会编译?

【问题讨论】:

    标签: c++ c++11 clang


    【解决方案1】:

    我用它编译了

    clang++ test.cpp  -std=c++11 -stdlib=libc++
    

    【讨论】:

    • +1 这个特定示例将在没有-std=c++11 的情况下编译。但是在 C++03 语言模式下对unique_ptr 的支持很弱,所以我推荐-std=c++11-stdlib=libc++ 选择 libc++ (libcxx.llvm.org)。如果没有这个标志,clang 默认使用 gcc-4.2 中的 libstdc++,它没有 C++11 库功能(除了 tr1 命名空间中的子集)。
    • 要演示霍华德所说的问题,请参阅No type named 'unique_ptr' in namespace 'std' when compiling under LLVM/Clang 的四个测试用例。
    猜你喜欢
    • 2015-12-20
    • 2014-12-07
    • 2019-07-10
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    相关资源
    最近更新 更多