Advertisement
ulfben

static asserts for Rule of 5 / regular types

Nov 26th, 2018
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. //https://philippegroarke.com/posts/2018/easy_defensive_programming/
  2. //http://www.modernescpp.com/index.php/c-core-guidelines-regular-and-semiregular-typs
  3. template <class T>
  4. constexpr bool fulfills_rule_of_5() {
  5.     static_assert(std::is_destructible_v<T>, "T : must be destructible");
  6.     static_assert(std::is_copy_constructible_v<T>, "T : must be copy constructible");
  7.     static_assert(std::is_move_constructible_v<T>, "T : must be move constructible");
  8.     static_assert(std::is_copy_assignable_v<T>, "T : must be copy assignable");
  9.     static_assert(std::is_move_assignable_v<T>, "T : must be move assignable");
  10.  
  11.     return std::is_destructible_v<T> && std::is_copy_constructible_v<T>
  12.         && std::is_move_constructible_v<T> && std::is_copy_assignable_v<T>
  13.         && std::is_move_assignable_v<T>;
  14. }
  15.  
  16. struct the_mighty_potato {
  17.     the_mighty_potato(int some_number, int mul) : carbs(some_number * mul) {}
  18.     int carbs{ 42 };
  19.     std::unique_ptr<int> calories{ nullptr };
  20. };
  21.  
  22. static_assert(fulfills_rule_of_5<the_mighty_potato>(), "the_might_potato: must fulfill rule of 5"); // Fails
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement