SHARE
TWEET

static asserts for Rule of 5 / regular types

ulfben Nov 26th, 2018 (edited) 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top