I
Imre
Hi
I'm trying to write a function template called CallF(), that has a type
argument called T. It has one argument, a T*. If T has a member
function F(), then CallF() should call it, otherwise it should do
nothing.
I've come up with a solution, that compiles well with the online Comeau
compiler, but not with VC++ 7.1 .
So my questions are:
- Is my stuff right according to the standard?
- Does anyone have a solution / workaround for this that works in VC?
Here's the code:
template <int I>
struct IntToVoid
{
typedef void type;
};
template <class T, typename Enable = void>
struct CallFHelper
{
static void F(T* t)
{
}
};
template <typename T>
int Test(int (T::*)())
{
return 42;
}
template <class T>
struct CallFHelper<T, typename IntToVoid<sizeof(Test<T>(T::F))>::type>
{
static void F(T* t)
{
t->F();
}
};
template <class T>
void CallF(T* t)
{
CallFHelper<T>::F(t);
}
// ---
class A
{
};
class B
{
public:
int F()
{
return 42;
}
};
int main(int argc, char** argv)
{
A a;
CallF(&a);
B b;
CallF(&b);
return 0;
}
Thanks,
Imre
I'm trying to write a function template called CallF(), that has a type
argument called T. It has one argument, a T*. If T has a member
function F(), then CallF() should call it, otherwise it should do
nothing.
I've come up with a solution, that compiles well with the online Comeau
compiler, but not with VC++ 7.1 .
So my questions are:
- Is my stuff right according to the standard?
- Does anyone have a solution / workaround for this that works in VC?
Here's the code:
template <int I>
struct IntToVoid
{
typedef void type;
};
template <class T, typename Enable = void>
struct CallFHelper
{
static void F(T* t)
{
}
};
template <typename T>
int Test(int (T::*)())
{
return 42;
}
template <class T>
struct CallFHelper<T, typename IntToVoid<sizeof(Test<T>(T::F))>::type>
{
static void F(T* t)
{
t->F();
}
};
template <class T>
void CallF(T* t)
{
CallFHelper<T>::F(t);
}
// ---
class A
{
};
class B
{
public:
int F()
{
return 42;
}
};
int main(int argc, char** argv)
{
A a;
CallF(&a);
B b;
CallF(&b);
return 0;
}
Thanks,
Imre