Clang support for C++11 and beyond

Sunday, April 21, 2013

Clang support for C++11 and beyond

As of r179861, Clang implements the entirety of the C++11 language standard. The following features have been implemented since the release of Clang 3.2, along with our plans for “C++1y”.

Support for [[attributes]]

C++11’s [[attribute]] syntax is now fully supported, including support for the standard [[noreturn]] and [[carries_dependency]] attributes (although [[carries_dependency]] does not provide improved code generation). This allows non-returning functions to be written with a standard syntax:

[[noreturn]] void foo() {
  while (true) do_something();
}

Just like __attribute__((noreturn)), Clang will warn you if you use this attribute on a function which can return, and will optimize callers on the assumption that the function does not return. Unlike __attribute__((noreturn)), [[noreturn]] is never part of a function’s type.

As with g++'s implementation, __attribute__((foo)) attributes which are supported by g++ can be written as [[gnu::foo]]. Clang-specific __attribute__((...))s are not available through this syntax (patches to add [[clang::...]] attribute names are welcome).

Clang also now provides complete support for C++11’s almost-attribute alignas(...).

Inheriting constructors

Clang now supports C++11’s inheriting constructor syntax, which provides a simple mechanism to re-export all the constructors from a base class, other than default constructors, or constructors which would be copy or move constructors for either the base or derived class. Example:

struct Base {
  Base(); // default constructor, not inherited
  Base(int, char);
  template<typename T> Base(T &x);
};
struct Derived : Base {
  using Base::Base;
};
Derived f(1, 'x');
Derived d("foo"); // ok, calls inheriting constructor template

thread_local variables

Clang now supports C++11’s thread_local keyword, including dynamic initialization and destruction of thread-local objects. Dynamic destruction requires a C++ runtime library which provides __cxa_thread_atexit, which is currently only provided by the g++4.8 C++ runtime library.

C++1y

With C++11 out of the door, what’s next? The C++ standardization committee voted yesterday to create the first Committee Draft for C++1y (which will very likely be C++14). Since this is only the first draft, there will probably be many minor changes before C++1y is done, but the rough feature set is unlikely to change much. This new language standard includes:

  • Generalized lambdas, allowing a templated call operator and arbitrary captures:

    auto apply = [v(21)] (auto &&fn) { fn(v); };
    apply([] (int &n) { n += 21; });
    apply([] (int n) { std::cout << n; });
    
  • Return type deduction for (non-lambda) functions:

    auto fn(int n) { return something(n); }
    
  • A more powerful

    constexpr

    feature, allowing variable mutation and loops:

    constexpr auto len(const char *str) {
      int k = 0;
      while (*str++) ++k;
      return k;
    }
    static_assert(len("foo") == 3, "hooray");
    

The improved constexpr``constexpr``const``constexpr``const``const

struct S {
  int n;
  constexpr int get() { return n; }
};

… as …

struct S {
  int n;
  constexpr int get() const { return n; }
};

… and your code will work in both C++11 and C++14. Clang already has a warning for code which is relying on the implicit rule, and will fix it for you if you run . Other compilers supporting C++11 are expected to start providing similar warnings soon.

Several of the new features were prototyped in Clang prior to standardization, and we expect implementations of those to land in Clang SVN over the coming weeks. See the Clang C++ status page for the latest details on C++1y features and Clang’s support for them. The implemented features can be enabled with the -std=c++1y command-line flag.

If you find bugs in the C++11 support, please report them on our bug tracker. If you want to get involved fixing bugs or working on C++1y support, our website has details of how you can help.

Posted by Richard Smith at 7:04 AM

Labels: C++, Clang, new-in-llvm-3.3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值