Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make static_assert block re-usable in template classes?

Say I have a template class that makes multiple static_asserts:

template <class T>
class Foo
{
    static_assert(!std::is_const<T>::value,"");
    static_assert(!std::is_reference<T>::value,"");
    static_assert(!std::is_pointer<T>::value,"");

    //...<snip>...
}

Now say I have more template classes that need to make the same asserts.

Is there a way to make a static_assert block reusable? A "static_assert function" if you will.

like image 490
Unimportant Avatar asked Oct 19 '25 15:10

Unimportant


1 Answers

You can just combine required traits into one with descriptive name:

template<typename T> using
is_fancy = ::std::integral_constant
<
    bool
,   (not std::is_const<T>::value)
    and
    (not std::is_reference<T>::value)
    and
    (not std::is_pointer<T>::value)
>;

and use it later:

static_assert(std::is_fancy<T>::value,"");
like image 57
user7860670 Avatar answered Oct 21 '25 05:10

user7860670