ExtendedCXX03Support.rst 3.8 KB

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