enable_special_members.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // <bits/enable_special_members.h> -*- C++ -*-
  2. // Copyright (C) 2013-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/enable_special_members.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly.
  23. */
  24. #ifndef _ENABLE_SPECIAL_MEMBERS_H
  25. #define _ENABLE_SPECIAL_MEMBERS_H 1
  26. #pragma GCC system_header
  27. #include <bits/c++config.h>
  28. namespace std _GLIBCXX_VISIBILITY(default)
  29. {
  30. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  31. /// @cond undocumented
  32. struct _Enable_default_constructor_tag
  33. {
  34. explicit constexpr _Enable_default_constructor_tag() = default;
  35. };
  36. /**
  37. * @brief A mixin helper to conditionally enable or disable the default
  38. * constructor.
  39. * @sa _Enable_special_members
  40. */
  41. template<bool _Switch, typename _Tag = void>
  42. struct _Enable_default_constructor
  43. {
  44. constexpr _Enable_default_constructor() noexcept = default;
  45. constexpr _Enable_default_constructor(_Enable_default_constructor const&)
  46. noexcept = default;
  47. constexpr _Enable_default_constructor(_Enable_default_constructor&&)
  48. noexcept = default;
  49. _Enable_default_constructor&
  50. operator=(_Enable_default_constructor const&) noexcept = default;
  51. _Enable_default_constructor&
  52. operator=(_Enable_default_constructor&&) noexcept = default;
  53. // Can be used in other ctors.
  54. constexpr explicit
  55. _Enable_default_constructor(_Enable_default_constructor_tag) { }
  56. };
  57. /**
  58. * @brief A mixin helper to conditionally enable or disable the default
  59. * destructor.
  60. * @sa _Enable_special_members
  61. */
  62. template<bool _Switch, typename _Tag = void>
  63. struct _Enable_destructor { };
  64. /**
  65. * @brief A mixin helper to conditionally enable or disable the copy/move
  66. * special members.
  67. * @sa _Enable_special_members
  68. */
  69. template<bool _Copy, bool _CopyAssignment,
  70. bool _Move, bool _MoveAssignment,
  71. typename _Tag = void>
  72. struct _Enable_copy_move { };
  73. /**
  74. * @brief A mixin helper to conditionally enable or disable the special
  75. * members.
  76. *
  77. * The @c _Tag type parameter is to make mixin bases unique and thus avoid
  78. * ambiguities.
  79. */
  80. template<bool _Default, bool _Destructor,
  81. bool _Copy, bool _CopyAssignment,
  82. bool _Move, bool _MoveAssignment,
  83. typename _Tag = void>
  84. struct _Enable_special_members
  85. : private _Enable_default_constructor<_Default, _Tag>,
  86. private _Enable_destructor<_Destructor, _Tag>,
  87. private _Enable_copy_move<_Copy, _CopyAssignment,
  88. _Move, _MoveAssignment,
  89. _Tag>
  90. { };
  91. // Boilerplate follows.
  92. template<typename _Tag>
  93. struct _Enable_default_constructor<false, _Tag>
  94. {
  95. constexpr _Enable_default_constructor() noexcept = delete;
  96. constexpr _Enable_default_constructor(_Enable_default_constructor const&)
  97. noexcept = default;
  98. constexpr _Enable_default_constructor(_Enable_default_constructor&&)
  99. noexcept = default;
  100. _Enable_default_constructor&
  101. operator=(_Enable_default_constructor const&) noexcept = default;
  102. _Enable_default_constructor&
  103. operator=(_Enable_default_constructor&&) noexcept = default;
  104. // Can be used in other ctors.
  105. constexpr explicit
  106. _Enable_default_constructor(_Enable_default_constructor_tag) { }
  107. };
  108. template<typename _Tag>
  109. struct _Enable_destructor<false, _Tag>
  110. { ~_Enable_destructor() noexcept = delete; };
  111. template<typename _Tag>
  112. struct _Enable_copy_move<false, true, true, true, _Tag>
  113. {
  114. constexpr _Enable_copy_move() noexcept = default;
  115. constexpr _Enable_copy_move(_Enable_copy_move const&) noexcept = delete;
  116. constexpr _Enable_copy_move(_Enable_copy_move&&) noexcept = default;
  117. _Enable_copy_move&
  118. operator=(_Enable_copy_move const&) noexcept = default;
  119. _Enable_copy_move&
  120. operator=(_Enable_copy_move&&) noexcept = default;
  121. };
  122. template<typename _Tag>
  123. struct _Enable_copy_move<true, false, true, true, _Tag>
  124. {
  125. constexpr _Enable_copy_move() noexcept = default;
  126. constexpr _Enable_copy_move(_Enable_copy_move const&) noexcept = default;
  127. constexpr _Enable_copy_move(_Enable_copy_move&&) noexcept = default;
  128. _Enable_copy_move&
  129. operator=(_Enable_copy_move const&) noexcept = delete;
  130. _Enable_copy_move&
  131. operator=(_Enable_copy_move&&) noexcept = default;
  132. };
  133. template<typename _Tag>
  134. struct _Enable_copy_move<false, false, true, true, _Tag>
  135. {
  136. constexpr _Enable_copy_move() noexcept = default;
  137. constexpr _Enable_copy_move(_Enable_copy_move const&) noexcept = delete;
  138. constexpr _Enable_copy_move(_Enable_copy_move&&) noexcept = default;
  139. _Enable_copy_move&
  140. operator=(_Enable_copy_move const&) noexcept = delete;
  141. _Enable_copy_move&
  142. operator=(_Enable_copy_move&&) noexcept = default;
  143. };
  144. template<typename _Tag>
  145. struct _Enable_copy_move<true, true, false, true, _Tag>
  146. {
  147. constexpr _Enable_copy_move() noexcept = default;
  148. constexpr _Enable_copy_move(_Enable_copy_move const&) noexcept = default;
  149. constexpr _Enable_copy_move(_Enable_copy_move&&) noexcept = delete;
  150. _Enable_copy_move&
  151. operator=(_Enable_copy_move const&) noexcept = default;
  152. _Enable_copy_move&
  153. operator=(_Enable_copy_move&&) noexcept = default;
  154. };
  155. template<typename _Tag>
  156. struct _Enable_copy_move<false, true, false, true, _Tag>
  157. {
  158. constexpr _Enable_copy_move() noexcept = default;
  159. constexpr _Enable_copy_move(_Enable_copy_move const&) noexcept = delete;
  160. constexpr _Enable_copy_move(_Enable_copy_move&&) noexcept = delete;
  161. _Enable_copy_move&
  162. operator=(_Enable_copy_move const&) noexcept = default;
  163. _Enable_copy_move&
  164. operator=(_Enable_copy_move&&) noexcept = default;
  165. };
  166. template<typename _Tag>
  167. struct _Enable_copy_move<true, false, false, true, _Tag>
  168. {
  169. constexpr _Enable_copy_move() noexcept = default;
  170. constexpr _Enable_copy_move(_Enable_copy_move const&) noexcept = default;
  171. constexpr _Enable_copy_move(_Enable_copy_move&&) noexcept = delete;
  172. _Enable_copy_move&
  173. operator=(_Enable_copy_move const&) noexcept = delete;
  174. _Enable_copy_move&
  175. operator=(_Enable_copy_move&&) noexcept = default;
  176. };
  177. template<typename _Tag>
  178. struct _Enable_copy_move<false, false, false, true, _Tag>
  179. {
  180. constexpr _Enable_copy_move() noexcept = default;
  181. constexpr _Enable_copy_move(_Enable_copy_move const&) noexcept = delete;
  182. constexpr _Enable_copy_move(_Enable_copy_move&&) noexcept = delete;
  183. _Enable_copy_move&
  184. operator=(_Enable_copy_move const&) noexcept = delete;
  185. _Enable_copy_move&
  186. operator=(_Enable_copy_move&&) noexcept = default;
  187. };
  188. template<typename _Tag>
  189. struct _Enable_copy_move<true, true, true, false, _Tag>
  190. {
  191. constexpr _Enable_copy_move() noexcept = default;
  192. constexpr _Enable_copy_move(_Enable_copy_move const&) noexcept = default;
  193. constexpr _Enable_copy_move(_Enable_copy_move&&) noexcept = default;
  194. _Enable_copy_move&
  195. operator=(_Enable_copy_move const&) noexcept = default;
  196. _Enable_copy_move&
  197. operator=(_Enable_copy_move&&) noexcept = delete;
  198. };
  199. template<typename _Tag>
  200. struct _Enable_copy_move<false, true, true, false, _Tag>
  201. {
  202. constexpr _Enable_copy_move() noexcept = default;
  203. constexpr _Enable_copy_move(_Enable_copy_move const&) noexcept = delete;
  204. constexpr _Enable_copy_move(_Enable_copy_move&&) noexcept = default;
  205. _Enable_copy_move&
  206. operator=(_Enable_copy_move const&) noexcept = default;
  207. _Enable_copy_move&
  208. operator=(_Enable_copy_move&&) noexcept = delete;
  209. };
  210. template<typename _Tag>
  211. struct _Enable_copy_move<true, false, true, false, _Tag>
  212. {
  213. constexpr _Enable_copy_move() noexcept = default;
  214. constexpr _Enable_copy_move(_Enable_copy_move const&) noexcept = default;
  215. constexpr _Enable_copy_move(_Enable_copy_move&&) noexcept = default;
  216. _Enable_copy_move&
  217. operator=(_Enable_copy_move const&) noexcept = delete;
  218. _Enable_copy_move&
  219. operator=(_Enable_copy_move&&) noexcept = delete;
  220. };
  221. template<typename _Tag>
  222. struct _Enable_copy_move<false, false, true, false, _Tag>
  223. {
  224. constexpr _Enable_copy_move() noexcept = default;
  225. constexpr _Enable_copy_move(_Enable_copy_move const&) noexcept = delete;
  226. constexpr _Enable_copy_move(_Enable_copy_move&&) noexcept = default;
  227. _Enable_copy_move&
  228. operator=(_Enable_copy_move const&) noexcept = delete;
  229. _Enable_copy_move&
  230. operator=(_Enable_copy_move&&) noexcept = delete;
  231. };
  232. template<typename _Tag>
  233. struct _Enable_copy_move<true, true, false, false, _Tag>
  234. {
  235. constexpr _Enable_copy_move() noexcept = default;
  236. constexpr _Enable_copy_move(_Enable_copy_move const&) noexcept = default;
  237. constexpr _Enable_copy_move(_Enable_copy_move&&) noexcept = delete;
  238. _Enable_copy_move&
  239. operator=(_Enable_copy_move const&) noexcept = default;
  240. _Enable_copy_move&
  241. operator=(_Enable_copy_move&&) noexcept = delete;
  242. };
  243. template<typename _Tag>
  244. struct _Enable_copy_move<false, true, false, false, _Tag>
  245. {
  246. constexpr _Enable_copy_move() noexcept = default;
  247. constexpr _Enable_copy_move(_Enable_copy_move const&) noexcept = delete;
  248. constexpr _Enable_copy_move(_Enable_copy_move&&) noexcept = delete;
  249. _Enable_copy_move&
  250. operator=(_Enable_copy_move const&) noexcept = default;
  251. _Enable_copy_move&
  252. operator=(_Enable_copy_move&&) noexcept = delete;
  253. };
  254. template<typename _Tag>
  255. struct _Enable_copy_move<true, false, false, false, _Tag>
  256. {
  257. constexpr _Enable_copy_move() noexcept = default;
  258. constexpr _Enable_copy_move(_Enable_copy_move const&) noexcept = default;
  259. constexpr _Enable_copy_move(_Enable_copy_move&&) noexcept = delete;
  260. _Enable_copy_move&
  261. operator=(_Enable_copy_move const&) noexcept = delete;
  262. _Enable_copy_move&
  263. operator=(_Enable_copy_move&&) noexcept = delete;
  264. };
  265. template<typename _Tag>
  266. struct _Enable_copy_move<false, false, false, false, _Tag>
  267. {
  268. constexpr _Enable_copy_move() noexcept = default;
  269. constexpr _Enable_copy_move(_Enable_copy_move const&) noexcept = delete;
  270. constexpr _Enable_copy_move(_Enable_copy_move&&) noexcept = delete;
  271. _Enable_copy_move&
  272. operator=(_Enable_copy_move const&) noexcept = delete;
  273. _Enable_copy_move&
  274. operator=(_Enable_copy_move&&) noexcept = delete;
  275. };
  276. /// @endcond
  277. _GLIBCXX_END_NAMESPACE_VERSION
  278. } // namespace std
  279. #endif // _ENABLE_SPECIAL_MEMBERS_H