Clang C++0x extensions

Is there a specific command option to enable Clang C++0x extensions? I have been looking through the manual and cannot find this.

John

I think you just want -std=c++0x .

John.

You probably want -std=gnu++0x.

You can use -std=c++0x, but that disables gnu extensions. The default flag is -std=gnu++03, and quite a bit of code (including last time I checked the standard header <cmath> on mac os x) won't compile in c++03 or c++0x mode.

Chris

Christopher

Both -std=gnu++0x and -std=c++0x give errors in the GNU headers for gcc 4.4 (the default version of gcc on my ubuntu system)

fletcher@fletcher-desktop:~/test$ gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

These errors may relate to c++0x things not yet implemented in Clang, or it could be that I need a different set of g++ headers. I cannot tell. Some problems are in <iostream> and some caused by clang/Basic/Version (clang 2.9)

John

fletcher@fletcher-desktop:~/test$ make testcpp_cpp0x
/home/fletcher/LLVM/llvm/bin/clang++ -I/home/fletcher/LLVM/llvm/include -std=gnu++0x -DCLANG testcpp.cpp -otestcpp_cpp0x -L/home/fletcher/LLVM/llvm/lib -lclangBasic -lLLVMSupport -lpthread -ldl
In file included from testcpp.cpp:47:
In file included from /usr/include/c++/4.4/iostream:39:
In file included from /usr/include/c++/4.4/ostream:39:
In file included from /usr/include/c++/4.4/ios:39:
In file included from /usr/include/c++/4.4/exception:148:
/usr/include/c++/4.4/exception_ptr.h:143:13: error: unknown type name
      'type_info'
      const type_info*
            ^
In file included from testcpp.cpp:47:
In file included from /usr/include/c++/4.4/iostream:39:
In file included from /usr/include/c++/4.4/ostream:39:
In file included from /usr/include/c++/4.4/ios:40:
In file included from /usr/include/c++/4.4/bits/char_traits.h:40:
In file included from /usr/include/c++/4.4/bits/stl_algobase.h:66:
/usr/include/c++/4.4/bits/stl_pair.h:255:27: error: no matching function for
      call to 'forward'
        (std::forward<_T1>(__x), std::forward<_T2>(__y));
                                 ^~~~~~~~~~~~~~~~~
In file included from testcpp.cpp:49:
In file included from /home/fletcher/LLVM/llvm/include/clang/Basic/Version.h:18:
/home/fletcher/LLVM/llvm/include/llvm/ADT/StringRef.h:363:16: note: in
      instantiation of function template specialization 'std::make_pair<const
      llvm::StringRef &, llvm::StringRef>' requested here
        return std::make_pair(*this, StringRef());
               ^
In file included from testcpp.cpp:47:
In file included from /usr/include/c++/4.4/iostream:39:
In file included from /usr/include/c++/4.4/ostream:39:
In file included from /usr/include/c++/4.4/ios:40:
In file included from /usr/include/c++/4.4/bits/char_traits.h:40:
In file included from /usr/include/c++/4.4/bits/stl_algobase.h:66:
In file included from /usr/include/c++/4.4/bits/stl_pair.h:60:
/usr/include/c++/4.4/bits/move.h:51:5: note: candidate function [with _Tp =
      llvm::StringRef] not viable: no known conversion from 'llvm::StringRef' to
      'typename std::identity<StringRef>::type &&' (aka 'llvm::StringRef &&')
      for 1st argument
    forward(typename std::identity<_Tp>::type&& __t)
    ^
/usr/include/c++/4.4/bits/move.h:57:14: error: binding of reference to type
      'typename std::remove_reference<StringRef &>::type'
      (aka 'llvm::StringRef') to a value of type 'llvm::StringRef' drops
      qualifiers
    { return __t; }
             ^~~
In file included from testcpp.cpp:47:
In file included from /usr/include/c++/4.4/iostream:39:
In file included from /usr/include/c++/4.4/ostream:39:
In file included from /usr/include/c++/4.4/ios:40:
In file included from /usr/include/c++/4.4/bits/char_traits.h:40:
In file included from /usr/include/c++/4.4/bits/stl_algobase.h:66:
/usr/include/c++/4.4/bits/stl_pair.h:93:15: note: in instantiation of function
      template specialization 'std::move<llvm::StringRef &>' requested here
      : first(std::move(__p.first)),
              ^
In file included from testcpp.cpp:49:
In file included from /home/fletcher/LLVM/llvm/include/clang/Basic/Version.h:18:
/home/fletcher/LLVM/llvm/include/llvm/ADT/StringRef.h:363:16: note: in
      instantiation of member function 'std::pair<llvm::StringRef,
      llvm::StringRef>::pair' requested here
        return std::make_pair(*this, StringRef());
               ^
In file included from testcpp.cpp:47:
In file included from /usr/include/c++/4.4/iostream:39:
In file included from /usr/include/c++/4.4/ostream:39:
In file included from /usr/include/c++/4.4/ios:40:
In file included from /usr/include/c++/4.4/bits/char_traits.h:40:
In file included from /usr/include/c++/4.4/bits/stl_algobase.h:66:
/usr/include/c++/4.4/bits/stl_pair.h:255:3: error: no matching function for call
      to 'forward'
        (std::forward<_T1>(__x), std::forward<_T2>(__y));
         ^~~~~~~~~~~~~~~~~
In file included from testcpp.cpp:49:
In file included from /home/fletcher/LLVM/llvm/include/clang/Basic/Version.h:18:
/home/fletcher/LLVM/llvm/include/llvm/ADT/StringRef.h:364:14: note: in
      instantiation of function template specialization
      'std::make_pair<llvm::StringRef, llvm::StringRef>' requested here
      return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
             ^
In file included from testcpp.cpp:47:
In file included from /usr/include/c++/4.4/iostream:39:
In file included from /usr/include/c++/4.4/ostream:39:
In file included from /usr/include/c++/4.4/ios:40:
In file included from /usr/include/c++/4.4/bits/char_traits.h:40:
In file included from /usr/include/c++/4.4/bits/stl_algobase.h:66:
In file included from /usr/include/c++/4.4/bits/stl_pair.h:60:
/usr/include/c++/4.4/bits/move.h:51:5: note: candidate function [with _Tp =
      llvm::StringRef] not viable: no known conversion from 'llvm::StringRef' to
      'typename std::identity<StringRef>::type &&' (aka 'llvm::StringRef &&')
      for 1st argument
    forward(typename std::identity<_Tp>::type&& __t)
    ^
4 errors generated.
make: *** [testcpp_cpp0x] Error 1

Hi,
   Interesting... I don't think it is only for 0x... I have the same problem:
uname -a
Linux PCPHSFT79 2.6.32-31-generic #61-Ubuntu SMP Fri Apr 8 18:25:51 UTC 2011 x86_64 GNU/Linux

cat clang_fails.cxx
#include <string>

extern "C" int printf(const char* fmt, ...);

int main () {
   std::string a = "a";
   printf("%s\n", a.c_str());
   return 0;
}

../Debug+Asserts/bin/clang clang_fails.cxx
In file included from clang_fails.cxx:1:
In file included from /usr/include/c++/4.4/string:41:
In file included from /usr/include/c++/4.4/bits/char_traits.h:40:
In file included from /usr/include/c++/4.4/bits/stl_algobase.h:65:
/usr/include/c++/4.4/ext/numeric_traits.h:60:25: error: expected member name or ';' after declaration specifiers
       static const bool __is_signed = __glibcxx_signed(_Value);
       ~~~~~~~~~~~~~~~~~ ^
/usr/include/c++/4.4/ext/numeric_traits.h:71:50: error: expected unqualified-id
     const bool __numeric_traits_integer<_Value>::__is_signed;
                                                  ^
/usr/include/c++/4.4/ext/numeric_traits.h:103:25: error: expected member name or ';' after declaration specifiers
       static const bool __is_signed = true;
       ~~~~~~~~~~~~~~~~~ ^
/usr/include/c++/4.4/ext/numeric_traits.h:112:51: error: expected unqualified-id
     const bool __numeric_traits_floating<_Value>::__is_signed;
                                                   ^
In file included from clang_fails.cxx:1:
In file included from /usr/include/c++/4.4/string:41:
In file included from /usr/include/c++/4.4/bits/char_traits.h:40:
/usr/include/c++/4.4/bits/stl_algobase.h:925:49: error: expected unqualified-id
&& !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
                                                        ^
/usr/include/c++/4.4/bits/stl_algobase.h:930:45: error: non-type template argument of type 'const bool' is not an integral constant expression
       return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
                                             ^~~~~~~~
6 errors generated.

I don't think this is expected. Is it?

Vassil

This is a recent regression, see http://llvm.org/bugs/show_bug.cgi?id=9804

ismail

I don't see this error. I only see errors when I define -std=gnu++0x or -std=c++0x with clang 2.9.

For me, Clang without the flags works, and also g++ with -std=gnu++0x

It looks as though some of the errors are addressed in this patch file: libstdc++4.4-clang0x.patch
but I have not seen anything there about type_info, the first of the errors I report.

John

I don't see this error. I only see errors when I define -std=gnu++0x or -std=c++0x with clang 2.9.

For me, Clang without the flags works, and also g++ with -std=gnu++0x

It looks as though some of the errors are addressed in this patch file: libstdc++4.4-clang0x.patch

Right. That patch is necessary because libstdc++ 4.4 was written to a previous formulation of rvalue references (before we removed the ability of rvalue references to bind to lvalues), but Clang implements rvalue references based on the C++0x FDIS.

but I have not seen anything there about type_info, the first of the errors I report.

It's a header-dependency bug in libstdc++-4.4, IIRC. GCC gets around it by (incorrectly) predefining std::type_info in the compiler. Just #include <typeinfo> in the appropriate place.

  - Doug

Doug

Thank you for that.

(1) The patch seems to be oriented to an Apple installation. Do those changes mean the headers won't work with g++ any more?

(2) Unfortunately, even including <typeinfo> fails as it includes <exception> before it defines type_info.

The solution for me is to put this in my code before including <iostream>:

namespace std {
  class type_info;
}

That at least fixes the first bug.

John

Doug

Thank you for that.

(1) The patch seems to be oriented to an Apple installation.

It should work on any libstdc++ 4.4; they don't change much from one platform to another.

Do those changes mean the headers won't work with g++ any more?

No.

(2) Unfortunately, even including <typeinfo> fails as it includes <exception> before it defines type_info.

The solution for me is to put this in my code before including <iostream>:

namespace std {
class type_info;
}

That at least fixes the first bug.

That's effectively what GCC does in the compiler.

  - Doug

Does clang work correctly with gcc 4.4?
I was unable to compile even a hello world program using gcc 4.4 headers with clang