473,804 Members | 3,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template function specialization/overloading

I am facing the following scenario: I have a class 'A', that implements
some concept C -- but we know this, not because A inherits from a
virtual class 'C', but only because a trait tell us so:

class A {};

template <typename T>
struct Is_C
{
static const bool value = false;
};

template <>
struct Is_C<A>
{
static const bool value = true;
};
What I would like to do is, given an instance 'a' of A, call a function
'foo(a)' that would have specific code for class A -- and have default
code for other classes.

The following

template < typename T >
void foo(T t)
{
if (Is_C<T>::value )
{
/* ... */
}
else
{
/* ... */
}
}

would work, but then, it is not extensible to another trait 'Is_D'
unless I modify the function foo. Ideally I would have a function foo
for the general case (corresponding to the 'else') and as many
functions foo for all the different tests, but I am not sure how to do
that. I would like to use the boost::enable_i f, but if I use function
overloading, I need to have a disable_if in my 'general case' foo for
each 'specialized' foo:

// General case
template < typename T >
typename disable_if<Is_C <T> >::type // This has to be modified if
another foo is added
foo(T t)
{
/* ... */
}

// Specialization for concept C
template < typename T >
typename enable_if<Is_C< T> >::type
foo(T t)
{
/* ... */
}

I would really appreciate any suggestion for this problem.

Thanks

B.

Jul 23 '05
11 2492
I am not sure what you mean. For the example I was mentioning, the
enable_if solution would be

// general case
template <typename T1, typename T2, typename T3 >
typename disable_if_c<
numeric_traits< T1>::is_special ized &&
numeric_traits< T2>::is_special ized &&
numeric_traits< T3>::is_special ized
::type foo(const T1 &x1, const T2 &x2, const T3 &x3)
{
/*...*/
}

// case when all types have numeric traits
template <typename T1, typename T2, typename T3 >
typename enable_if_c<
numeric_traits< T1>::is_special ized &&
numeric_traits< T2>::is_special ized &&
numeric_traits< T3>::is_special ized::type

foo(const T1 &x1, const T2 &x2, const T3 &x3)
{
/*...*/
}

What would be the equivalent with your technique? I can see that I
would need only one tag, for the case when all types have numeric
traits, but how would I avoid having a trait with three template
arguments, and having to specialize this trait for all 3-uples of types
with known numeric traits (and there are a lot of them) ? How could I
reuse numeric traits?

Bolin

Jul 23 '05 #11
ga*******@voila .fr wrote:
I am not sure what you mean. For the example I was mentioning, the
enable_if solution would be

// general case
template <typename T1, typename T2, typename T3 >
typename disable_if_c<
numeric_traits< T1>::is_special ized &&
numeric_traits< T2>::is_special ized &&
numeric_traits< T3>::is_special ized
::type

foo(const T1 &x1, const T2 &x2, const T3 &x3)
{
/*...*/
}

// case when all types have numeric traits
template <typename T1, typename T2, typename T3 >
typename enable_if_c<
numeric_traits< T1>::is_special ized &&
numeric_traits< T2>::is_special ized &&
numeric_traits< T3>::is_special ized
::type

foo(const T1 &x1, const T2 &x2, const T3 &x3)
{
/*...*/
}

What would be the equivalent with your technique? I can see that I
would need only one tag, for the case when all types have numeric
traits, but how would I avoid having a trait with three template
arguments, and having to specialize this trait for all 3-uples of types
with known numeric traits (and there are a lot of them) ? How could I
reuse numeric traits?

Bolin


Create a function like this:
template <typename T1, typename T2, typename T3>
void foo(const T1& x1, IsSpecializedTa g,
const T2& x2, IsSpecializedTa g,
const T3& x3, IsSpecializedTa g)
{
/* ... */
}

template <typename T1, typename T2, typename T3>
void foo(const T1& x1, const T2& x2, const T3& x3)
{
typedef typename Trait<T1>::Tag Tag1;
typedef typename Trait<T2>::Tag Tag2;
typedef typename Trait<T3>::Tag Tag3;

return foo(x1, Tag1(),
x2, Tag2(),
x3, Tag3());
}
Of course, you also have the flexibility to mix and match tags like
this:
template <typename T1, typename T2, typename T3>
void foo(const T1& x1, IsSpecializedTa g,
const T2& x2, IsSomethingElse Tag,
const T3& x3, IsYetSomethingE lseTag)
{
/* ... */
}
and 'foo' will "just work" as long as you have assigned an appropriate
tag for each of the possible types (via specialization) .

Hope this helps,
-shez-

Jul 23 '05 #12

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

Similar topics

17
6869
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section 18.3.1, 'Iseq'). I want the version for containers that he gives, but also to provide a specialization for construction from a pair<It,It> (eg because that is returned by equal_range()).
2
5781
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem which I can't figure out. I have a simple class C with 2 template member objects, and a function print() that prints the value of these objects. I defined a specialization of print() for C<string, char> which is called correctly. Then I wanted...
8
6743
by: Massimiliano Alberti | last post by:
Can I specialize a template function in a subclass without overriding it? (the main template function is defined in a base class). Now I'm doing something like that: (in base class) template<class X> void myfunc(X par1) { } (in derived class) template<class X>
5
6590
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
2
3690
by: Michael Stembera | last post by:
Here is a very simple piece of code to repro this bug. template<typename T, int N> inline bool foo( void ) { return true; } template<typename T> inline bool foo<T, 1>( void ) { return false;
6
5017
by: flopbucket | last post by:
Could someone explain to me what the difference is between function template specialization and function overloading? I guess overloading can change the number of parameters, but otherwise they seem very similar to me - i.e. they both provide specialized functions for depending on the parameter types. Thanks
4
3357
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
8
1950
by: mattias.nissler | last post by:
Hi! Here is a problem I ran into at work. The following example doesn't compile on gcc-4.1: struct cons_end {}; template<typename U,typename Vstruct cons { U elem; V tail;
13
6601
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm trying to use from an executable, compile using gcc 4.1.2. Everything works fine until I try specializing one of the static member function templates in a non-template class. I have a feeling I'm messing up something obvious so before I post a...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10076
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
9144
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...
1
7616
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
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
3
2990
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.