xref: /openbsd-src/gnu/llvm/libcxx/docs/DesignDocs/ExtendedCXX03Support.rst (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
146035553Spatrick=======================
246035553SpatrickExtended C++03 Support
346035553Spatrick=======================
446035553Spatrick
546035553Spatrick.. contents::
646035553Spatrick   :local:
746035553Spatrick
846035553SpatrickOverview
946035553Spatrick========
1046035553Spatrick
1146035553Spatricklibc++ is an implementation of the C++ standard library targeting C++11 or later.
1246035553Spatrick
1346035553SpatrickIn C++03, the library implements the C++11 standard using C++11 language extensions provided
1446035553Spatrickby Clang.
1546035553Spatrick
1646035553SpatrickThis document tracks the C++11 extensions libc++ requires, the C++11 extensions it provides,
1746035553Spatrickand how to write minimal C++11 inside libc++.
1846035553Spatrick
1946035553SpatrickRequired C++11 Compiler Extensions
2046035553Spatrick==================================
2146035553Spatrick
2246035553SpatrickClang provides a large subset of C++11 in C++03 as an extension. The features
2346035553Spatricklibc++ expects Clang  to provide are:
2446035553Spatrick
2546035553Spatrick* Variadic templates.
2646035553Spatrick* RValue references and perfect forwarding.
2746035553Spatrick* Alias templates
2846035553Spatrick* defaulted and deleted Functions.
2946035553Spatrick* reference qualified Functions
30*4bdff4beSrobert* ``auto``
3146035553Spatrick
3246035553SpatrickThere are also features that Clang *does not* provide as an extension in C++03
3346035553Spatrickmode. These include:
3446035553Spatrick
3546035553Spatrick* ``constexpr`` and ``noexcept``
3646035553Spatrick*  Trailing return types.
3746035553Spatrick* ``>>`` without a space.
3846035553Spatrick
3946035553Spatrick
4046035553SpatrickProvided C++11 Library Extensions
4146035553Spatrick=================================
4246035553Spatrick
4346035553Spatrick.. warning::
4446035553Spatrick  The C++11 extensions libc++ provides in C++03 are currently undergoing change. Existing extensions
4546035553Spatrick  may be removed in the future. New users are strongly discouraged depending on these extension
4646035553Spatrick  in new code.
4746035553Spatrick
4846035553Spatrick  This section will be updated once the libc++ developer community has further discussed the
4946035553Spatrick  future of C++03 with libc++.
5046035553Spatrick
5146035553Spatrick
5246035553SpatrickUsing Minimal C++11 in libc++
5346035553Spatrick=============================
5446035553Spatrick
5546035553SpatrickThis section is for developers submitting patches to libc++. It describes idioms that should be
5646035553Spatrickused in libc++ code, even in C++03, and the reasons behind them.
5746035553Spatrick
5846035553Spatrick
5946035553SpatrickUse Alias Templates over Class Templates
6046035553Spatrick----------------------------------------
6146035553Spatrick
6246035553SpatrickAlias templates should be used instead of class templates in metaprogramming. Unlike class templates,
6346035553SpatrickAlias templates do not produce a new instantiation every time they are used. This significantly
6446035553Spatrickdecreases the amount of memory used by the compiler.
6546035553Spatrick
6646035553SpatrickFor example, libc++ should not use ``add_const`` internally. Instead it should use an alias template
6746035553Spatricklike
6846035553Spatrick
6946035553Spatrick.. code-block:: cpp
7046035553Spatrick
7146035553Spatrick  template <class _Tp>
7246035553Spatrick  using _AddConst = const _Tp;
7346035553Spatrick
7446035553SpatrickUse Default Template Parameters for SFINAE
7546035553Spatrick------------------------------------------
7646035553Spatrick
7746035553SpatrickThere are three places in a function declaration that SFINAE may occur: In the template parameter list,
7846035553Spatrickin the function parameter list, and in the return type. For example:
7946035553Spatrick
8046035553Spatrick.. code-block:: cpp
8146035553Spatrick
8246035553Spatrick  template <class _Tp, class _ = enable_if_t</*...*/ >
8346035553Spatrick  void foo(_Tp); // #1
8446035553Spatrick
8546035553Spatrick  template <class _Tp>
8646035553Spatrick  void bar(_Tp, enable_if_t</*...*/>* = nullptr); // # 2
8746035553Spatrick
8846035553Spatrick  template <class _Tp>
8946035553Spatrick  enable_if_t</*...*/> baz(_Tp); // # 3
9046035553Spatrick
9146035553SpatrickUsing default template parameters for SFINAE (#1) should always be prefered.
9246035553Spatrick
9346035553SpatrickOption #2 has two problems. First, users can observe and accidentally pass values to the SFINAE
9446035553Spatrickfunction argument. Second, the default arguement creates a live variable, which causes debug
9546035553Spatrickinformation to be emitted containing the text of the SFINAE.
9646035553Spatrick
9746035553SpatrickOption #3 can also cause more debug information to be emitted than is needed, because the function
9846035553Spatrickreturn type will appear in the debug information.
9946035553Spatrick
10046035553SpatrickUse ``unique_ptr`` when allocating memory
10146035553Spatrick------------------------------------------
10246035553Spatrick
10346035553SpatrickThe standard library often needs to allocate memory and then construct a user type in it.
10446035553SpatrickIf the users constructor throws, the library needs to deallocate that memory. The idiomatic way to
10546035553Spatrickachieve this is with ``unique_ptr``.
10646035553Spatrick
10746035553Spatrick``__builtin_new_allocator`` is an example of this idiom. Example usage would look like:
10846035553Spatrick
10946035553Spatrick.. code-block:: cpp
11046035553Spatrick
11146035553Spatrick  template <class T>
11246035553Spatrick  T* __create() {
11346035553Spatrick    using _UniquePtr = unique_ptr<void*, __default_new_allocator::__default_new_deleter>;
11446035553Spatrick    _UniquePtr __p = __default_new_allocator::__allocate_bytes(sizeof(T), alignof(T));
11546035553Spatrick    T* __res = ::new(__p.get()) T();
11646035553Spatrick    (void)__p.release();
11746035553Spatrick    return __res;
11846035553Spatrick  }
119