clang 3.1看不到unique_ptr?

Gra*_*eme 16 c++ clang c++11

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

#include <memory>
#include <iostream>

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

当我编译时,我得到以下错误:

$ 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.
Run Code Online (Sandbox Code Playgroud)

我在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
Run Code Online (Sandbox Code Playgroud)

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

And*_*gia 27

我用它来编译它

clang++ test.cpp  -std=c++11 -stdlib=libc++
Run Code Online (Sandbox Code Playgroud)

  • +1这个特殊的例子将在没有`-std = c ++ 11`的情况下编译.但是对于`unique_ptr`的支持在C++ 03语言模式中很弱,所以我推荐`-std = c ++ 11`.`-stdlib = libc ++`选择libc ++(http://libcxx.llvm.org).没有这个标志,clang默认使用gcc-4.2中的libstdc ++,它没有C++ 11库特性(除了tr1命名空间中的子集). (11认同)