lundi 22 mai 2017

static_assert that a member variable is marked as 'static' in C++17?

I've been playing around with templates and would like to enforce that a template type given to a function has a particular static member. Unfortunately, there is no std::is_static type trait.

A rough example of how I would use it (or an alternative):

template<typename T>
void SomeFunc(T& obj)
{
    static_assert(std::is_static_v<decltype(T::someVariable)>, "someVariable must be static");
}

Are there any ways to achieve this kind of behaviour? I could just write

template<typename T>
void SomeFunc(T& obj)
{
    // must be static
    T::someVariable;
}

But this wouldn't be anywhere near as nice or informative as a static_assert with a decent message. This is just a syntax error of sorts :(

Thanks!

Aucun commentaire:

Enregistrer un commentaire