ulfben

static asserts for Rule of 5 / regular types

Nov 26th, 2018
116
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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×