Is there a way to use Substitution failure is not an error (SFINAE) for enum?
template <typename T>
struct Traits
{
}
template <>
struct Traits<A>
{
};
template <>
struct Traits<B>
{
enum
{
iOption = 1
};
};
template <T>
void Do()
{
// use Traits<T>::iOption
};
Then, Do<B>();
works and Do<A>();
fails. However, I can supply a default behavior when iOption does not exist.
So I separate out some part of Do to DoOption.
template <typename T, bool bOptionExist>
void DoOption()
{
// can't use Traits<T>::iOption. do some default behavior
};
template <typename T>
void DoOption<T, true>()
{
// use Traits<T>::iOption
};
template <T>
void Do()
{
// 'Do' does not use Traits<T>::iOption. Such codes are delegated to DoOption.
DoOption<T, DoesOptionExist<T> >();
};
Now, the missing piece is DoesOptionExist<T>
- a way to check whether iOption exists in the struct.
Certainly SFINAE works for function name or function signature, but not sure
it works for enum value.