ulfben icon

static asserts for Rule of 5 / regular types

ulfben | PRO | 11/26/18 11:19:07 AM UTC | 0 ⭐ | 1232 👁️ | Never ⏰ | []
C++ |

1.05 KB

|

None

|

0 👍

/

0 👎

//https://philippegroarke.com/posts/2018/easy_defensive_programming/
//http://www.modernescpp.com/index.php/c-core-guidelines-regular-and-semiregular-typs
template <class T>
constexpr bool fulfills_rule_of_5() {
    static_assert(std::is_destructible_v<T>, "T : must be destructible");
    static_assert(std::is_copy_constructible_v<T>, "T : must be copy constructible");
    static_assert(std::is_move_constructible_v<T>, "T : must be move constructible");
    static_assert(std::is_copy_assignable_v<T>, "T : must be copy assignable");
    static_assert(std::is_move_assignable_v<T>, "T : must be move assignable");
 
    return std::is_destructible_v<T> && std::is_copy_constructible_v<T>
        && std::is_move_constructible_v<T> && std::is_copy_assignable_v<T>
        && std::is_move_assignable_v<T>;
}
 
struct the_mighty_potato {
    the_mighty_potato(int some_number, int mul) : carbs(some_number * mul) {}
    int carbs{ 42 };
    std::unique_ptr<int> calories{ nullptr };
};
 
static_assert(fulfills_rule_of_5<the_mighty_potato>(), "the_might_potato: must fulfill rule of 5"); // Fails

Comments

  • Gyqizv icon
    04/02/26 06:20:48 PM UTC
    text |

    0 B

    |

    0 👍

    /

    0 👎

    Best t33n active cpx link daily updates!
     Copy and paste the link in a new browser tab and then hit enter!
     https://crazyporn.@xxx/members/157354/?asgtbndr=1&play=true&spon=adworld&zone=400
     Remove the @ 
  • Yuizcko icon
    05/18/26 01:08:05 PM UTC
    text |

    0 B

    |

    0 👍

    /

    0 👎

    Best t33n active cpx link daily updates!
     Copy and paste the link in a new browser tab and then hit enter!
     https://crazyporn.xxx/members/157354/?asgtbndr=1&play=true&spon=adworld&zone=400