The most general solution I can come up with works with any class template taking a bool
and an arbitrary amount of type parameters - including std::enable_if
:
template<template<bool, typename...> class T, bool arg1, typename... args>
struct Not : T<!arg1, args...> { };
Simple usage would be:
struct bacon {
static const bool tasty = true;
static std::string name() { return "bacon"; }
};
struct spinach {
static const bool tasty = false;
static std::string name() { return "spinach"; }
};
template<typename Food>
typename std::enable_if<Food::tasty>::type eat()
{
::std::cout << "Yummy! " << Food::name() << "!\n";
}
template<typename Food>
typename Not<std::enable_if, Food::tasty, void>::type eat()
{
::std::cout << "Yuck! " << Food::name() << "!\n";
}
And the test case
eat<bacon>();
eat<spinach>();
Will inform you that bacon
is tasty, while spinach
is not.