473,322 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Specialize a template based on the existance of a member function


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

May 24 '06 #1
2 1889
Imre <jr**@pager.hu> wrote:

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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
May 25 '06 #2
Imre wrote:
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

May 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Peter Koch Larsen | last post by:
This question has undoubtedly come up zillions of times before, but I've not been able to google for an answer. My problem can be reproduced as follows: template<typename T, bool b> struct bad...
1
by: mrstephengross | last post by:
Ok, I've got a class with two template parameters (A and B), and a member function foo(). I want to specialize foo for a particular class A. Is this possible? The following code shows an example: ...
1
by: sebastian | last post by:
Hi, I'd like to specialize a template function that contains a template parameter. In Example i have the following function declared: .... template < int i > static stupid_object&...
1
by: jn | last post by:
Hi everyone, I was trying to implement a design when I've found what seems a limitation of standard C++. I can't specialize an inner class in this way: template <class ARG> template <>...
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
4
by: stinos | last post by:
Hi All! suppose a class having a function for outputting data somehow, class X { template< class tType > void Output( const tType& arg ) { //default ToString handles integers/doubles
16
by: PengYu.UT | last post by:
Hi, I want to partial specialize the member function doit. But it doesn't work. Could you please help me to figure out what is wrong? Thanks, Peng template <typename T> class A {
7
by: mathieu | last post by:
Hi there, I know this is not possible in c++. So my question, how should I rewrite the following piece of code (without using a dummy class which template parameter could be use for partial...
5
by: huili80 | last post by:
For example, like in the following, the part commented out was intended as partial spectialzation, but it would even compile. Is it even legal to partially specialize a nested template class...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.