mardi 27 septembre 2016

How to simplify complicated SFINAE syntax, in pre-C++11, C++11, 14 and 17?

This question was inspired by this answer. I wonder what are/were the best ways to simplify in given standards. One I know and personally used/still use since C++11 is macro REQUIRES(x):

With definition:

template<long N>
struct requires_enum
{
    enum class type
    {
        none,
        all
    };
};

#define REQUIRES(...) requires_enum<__LINE__>::type = \
                      requires_enum<__LINE__>::type::none, \
                      bool PrivateBool = true, \
                      typename std::enable_if<PrivateBool && (__VA_ARGS__), int>::type = 0

And use if even for non-templated function calls:

template<REQUIRES(sizeof(int)==4)>
int fun() {return 0;}

int main()
{ 
    fun(); //only if sizeof(int)==4
}

What are the other good techniques?

Aucun commentaire:

Enregistrer un commentaire