16

I've just started playing around with clang and tried to compile the following sample program:

#include <memory>
#include <iostream>

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

When I compile I get the following errors:

$ 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.

I am using Clang 3.1 on Mac OS X:

$ 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

Any ideas why this wouldn't compile?

Graeme
  • 4,514
  • 5
  • 43
  • 71

1 Answers1

27

I got it to compile by using

clang++ test.cpp  -std=c++11 -stdlib=libc++
bames53
  • 86,085
  • 15
  • 179
  • 244
Andrea Bergia
  • 5,502
  • 1
  • 23
  • 38
  • 11
    +1 This particular example will compile without `-std=c++11`. However support for `unique_ptr` is weak in C++03 language mode, so I recommend the `-std=c++11`. The `-stdlib=libc++` chooses libc++ (http://libcxx.llvm.org). Without this flag clang defaults to using the libstdc++ from gcc-4.2, which has no C++11 library features (aside from a subset in the tr1 namespace). – Howard Hinnant Aug 07 '12 at 21:12
  • 1
    To demonstrate the problem Howard is talking about, see the four test cases at [No type named 'unique_ptr' in namespace 'std' when compiling under LLVM/Clang](http://stackoverflow.com/q/31655462). – jww Jul 29 '15 at 11:11