1*4d6fc14bSjoerg======================= 2*4d6fc14bSjoergExtended C++03 Support 3*4d6fc14bSjoerg======================= 4*4d6fc14bSjoerg 5*4d6fc14bSjoerg.. contents:: 6*4d6fc14bSjoerg :local: 7*4d6fc14bSjoerg 8*4d6fc14bSjoergOverview 9*4d6fc14bSjoerg======== 10*4d6fc14bSjoerg 11*4d6fc14bSjoerglibc++ is an implementation of the C++ standard library targeting C++11 or later. 12*4d6fc14bSjoerg 13*4d6fc14bSjoergIn C++03, the library implements the C++11 standard using C++11 language extensions provided 14*4d6fc14bSjoergby Clang. 15*4d6fc14bSjoerg 16*4d6fc14bSjoergThis document tracks the C++11 extensions libc++ requires, the C++11 extensions it provides, 17*4d6fc14bSjoergand how to write minimal C++11 inside libc++. 18*4d6fc14bSjoerg 19*4d6fc14bSjoergRequired C++11 Compiler Extensions 20*4d6fc14bSjoerg================================== 21*4d6fc14bSjoerg 22*4d6fc14bSjoergClang provides a large subset of C++11 in C++03 as an extension. The features 23*4d6fc14bSjoerglibc++ expects Clang to provide are: 24*4d6fc14bSjoerg 25*4d6fc14bSjoerg* Variadic templates. 26*4d6fc14bSjoerg* RValue references and perfect forwarding. 27*4d6fc14bSjoerg* Alias templates 28*4d6fc14bSjoerg* defaulted and deleted Functions. 29*4d6fc14bSjoerg* reference qualified Functions 30*4d6fc14bSjoerg 31*4d6fc14bSjoergThere are also features that Clang *does not* provide as an extension in C++03 32*4d6fc14bSjoergmode. These include: 33*4d6fc14bSjoerg 34*4d6fc14bSjoerg* ``constexpr`` and ``noexcept`` 35*4d6fc14bSjoerg* ``auto`` 36*4d6fc14bSjoerg* Trailing return types. 37*4d6fc14bSjoerg* ``>>`` without a space. 38*4d6fc14bSjoerg 39*4d6fc14bSjoerg 40*4d6fc14bSjoergProvided C++11 Library Extensions 41*4d6fc14bSjoerg================================= 42*4d6fc14bSjoerg 43*4d6fc14bSjoerg.. warning:: 44*4d6fc14bSjoerg The C++11 extensions libc++ provides in C++03 are currently undergoing change. Existing extensions 45*4d6fc14bSjoerg may be removed in the future. New users are strongly discouraged depending on these extension 46*4d6fc14bSjoerg in new code. 47*4d6fc14bSjoerg 48*4d6fc14bSjoerg This section will be updated once the libc++ developer community has further discussed the 49*4d6fc14bSjoerg future of C++03 with libc++. 50*4d6fc14bSjoerg 51*4d6fc14bSjoerg 52*4d6fc14bSjoergUsing Minimal C++11 in libc++ 53*4d6fc14bSjoerg============================= 54*4d6fc14bSjoerg 55*4d6fc14bSjoergThis section is for developers submitting patches to libc++. It describes idioms that should be 56*4d6fc14bSjoergused in libc++ code, even in C++03, and the reasons behind them. 57*4d6fc14bSjoerg 58*4d6fc14bSjoerg 59*4d6fc14bSjoergUse Alias Templates over Class Templates 60*4d6fc14bSjoerg---------------------------------------- 61*4d6fc14bSjoerg 62*4d6fc14bSjoergAlias templates should be used instead of class templates in metaprogramming. Unlike class templates, 63*4d6fc14bSjoergAlias templates do not produce a new instantiation every time they are used. This significantly 64*4d6fc14bSjoergdecreases the amount of memory used by the compiler. 65*4d6fc14bSjoerg 66*4d6fc14bSjoergFor example, libc++ should not use ``add_const`` internally. Instead it should use an alias template 67*4d6fc14bSjoerglike 68*4d6fc14bSjoerg 69*4d6fc14bSjoerg.. code-block:: cpp 70*4d6fc14bSjoerg 71*4d6fc14bSjoerg template <class _Tp> 72*4d6fc14bSjoerg using _AddConst = const _Tp; 73*4d6fc14bSjoerg 74*4d6fc14bSjoergUse Default Template Parameters for SFINAE 75*4d6fc14bSjoerg------------------------------------------ 76*4d6fc14bSjoerg 77*4d6fc14bSjoergThere are three places in a function declaration that SFINAE may occur: In the template parameter list, 78*4d6fc14bSjoergin the function parameter list, and in the return type. For example: 79*4d6fc14bSjoerg 80*4d6fc14bSjoerg.. code-block:: cpp 81*4d6fc14bSjoerg 82*4d6fc14bSjoerg template <class _Tp, class _ = enable_if_t</*...*/ > 83*4d6fc14bSjoerg void foo(_Tp); // #1 84*4d6fc14bSjoerg 85*4d6fc14bSjoerg template <class _Tp> 86*4d6fc14bSjoerg void bar(_Tp, enable_if_t</*...*/>* = nullptr); // # 2 87*4d6fc14bSjoerg 88*4d6fc14bSjoerg template <class _Tp> 89*4d6fc14bSjoerg enable_if_t</*...*/> baz(_Tp); // # 3 90*4d6fc14bSjoerg 91*4d6fc14bSjoergUsing default template parameters for SFINAE (#1) should always be prefered. 92*4d6fc14bSjoerg 93*4d6fc14bSjoergOption #2 has two problems. First, users can observe and accidentally pass values to the SFINAE 94*4d6fc14bSjoergfunction argument. Second, the default arguement creates a live variable, which causes debug 95*4d6fc14bSjoerginformation to be emitted containing the text of the SFINAE. 96*4d6fc14bSjoerg 97*4d6fc14bSjoergOption #3 can also cause more debug information to be emitted than is needed, because the function 98*4d6fc14bSjoergreturn type will appear in the debug information. 99*4d6fc14bSjoerg 100*4d6fc14bSjoergUse ``unique_ptr`` when allocating memory 101*4d6fc14bSjoerg------------------------------------------ 102*4d6fc14bSjoerg 103*4d6fc14bSjoergThe standard library often needs to allocate memory and then construct a user type in it. 104*4d6fc14bSjoergIf the users constructor throws, the library needs to deallocate that memory. The idiomatic way to 105*4d6fc14bSjoergachieve this is with ``unique_ptr``. 106*4d6fc14bSjoerg 107*4d6fc14bSjoerg``__builtin_new_allocator`` is an example of this idiom. Example usage would look like: 108*4d6fc14bSjoerg 109*4d6fc14bSjoerg.. code-block:: cpp 110*4d6fc14bSjoerg 111*4d6fc14bSjoerg template <class T> 112*4d6fc14bSjoerg T* __create() { 113*4d6fc14bSjoerg using _UniquePtr = unique_ptr<void*, __default_new_allocator::__default_new_deleter>; 114*4d6fc14bSjoerg _UniquePtr __p = __default_new_allocator::__allocate_bytes(sizeof(T), alignof(T)); 115*4d6fc14bSjoerg T* __res = ::new(__p.get()) T(); 116*4d6fc14bSjoerg (void)__p.release(); 117*4d6fc14bSjoerg return __res; 118*4d6fc14bSjoerg } 119