Specialize a template based on the existance of a member function

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
 
C

Christopher Benson-Manica

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.
- Is my stuff right according to the standard?

I am not a guru, so I will say only that I imagine that it isn't,
(although VC is not the poster-child for a standard-conforming
implementation).
- Does anyone have a solution / workaround for this that works in VC?

Again, I am not a guru, but I will offer something anyway, since it
seems that the gurus have not yet seen your post. At least one
possibility is to force all the T's that your template accepts to
inherit from a common base class,

class CallFClass
{
public:
virtual void F() {}
};

and then let classes that wish to implement F() do so. I am betting
that there is some common trait shared by the classes you wish to use
your template with, so introducing a common base class might not be a
bad design decision anyway.
 
M

mlimber

Imre said:
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?
[snip]

I'm not sure. You might try on the Boost User's list
(http://boost.org/more/mailing_lists.htm#users), as they might have a
way with Boost.MPL and/or Boost.TypeTraits.

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top