I am trying to disable some functions inside a simple template class. The functions to be deleted depend on whether the template argument has a specific typedef.
An example is as follows:
template<typename T>
struct Foo
{
typename T::Nested foo() { return typename T::Nested(); }
int bar() { return 1; }
};
struct NoNested
{
};
struct WithNested
{
typedef int Nested;
};
int main()
{
Foo<WithNested> fwn;
fwn.foo();
fwn.bar();
Foo<NoNested> fnn;
fnn.bar();
}
However, this gives me a style error error: no type named ‘Nested’ in ‘struct NoNested’for gcc and clang ++ (remember the old versions of both).
Is there an easy way to remove foowhen typedef T::Nestedfails? (In addition to the template specialization of the class Foo<T>, as in real code, I have this for about 5 functions with different typedefs .., which will lead to 2 ^ 5 different specializations)
EDIT:
Since some of them ask for motivation to want to do this: I would like to create something like FSM for use in DSL.
I would like to be able to do this
struct StateA;
struct StateB;
struct StateC;
struct StateA
{
typedef StateB AfterNext;
};
struct StateB
{
typedef StateA AfterPrev;
typedef StateC AfterNext;
};
struct StateC
{
typedef StateB AfterPrev;
};
template<typename T>
struct FSM
{
FSM<typename T::AfterNext> next() { return FSM<T::AfterNext>(); };
FSM<typename T::AfterPrev> prev() { return FSM<T::AfterPrev>(); };
};
So,
FSM<StateA>().next().prev().next().next();
,
FSM<StateA>().next().prev().prev();
.
, , , - , FSM - .
UPDATE:
, , .
, - , , , ( ), ( ) - , .