473,698 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<sizeo f(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 1908
Imre <jr**@pager.h u> 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)cybers pace.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.TypeTrait s.

Cheers! --M

May 25 '06 #3

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

Similar topics

9
1878
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 { void f(T const& t); }; // wanting to specialise for b = true
1
1394
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: ===================================== template<typename A, typename B> class Foo { int foo() { return 5; } };
1
1403
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& doSomething( int i, const another_stupid_object& __aso); ....
1
1682
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 <> class Outer<ARG>::Inner<int> {
3
2917
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 T,typename Alloc = std::allocator<T class CircularBuffer4 { public: typedef typename CircularBuffer<T,Alloc>::size_type
4
3354
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
3951
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
1926
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 specialization). template <typename TOut, typename TIn> void InverseRescaleFunction(TOut *out, const TIn *in, double intercept, double slope, size_t size) { ... }
5
3737
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 inside another template class? template < typename T > struct A { template < typename U > struct B
0
9152
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9016
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8856
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4613
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2321
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.