473,569 Members | 2,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template functions partial specialization

Hi!

I would like to know if it is possible to implement something like
this:

template <class X, class Y>
X void func(X, Y) { ... }

template <class X, class Y>
Y void func(X, Y) { ... }

The idea is that the compiler would choose the appropriate function
depending on the types of X and Y provided there exists some
compile-time criterium.

For example, given the concrete classes A and B (whereas B is derived
from A) I want to make sure that no matter in what order you provide
arguments to func(), the return value would be always A.

One of the solutions would be to put func() inside a template class and
use partial specialization, but that would not work if you deal with
overloading operators.

Are there any other options?

Thanks,
Andrei

May 5 '06 #1
5 2097
Andrei Tarassov wrote:
Hi!

I would like to know if it is possible to implement something like
this:

template <class X, class Y>
X void func(X, Y) { ... }

template <class X, class Y>
Y void func(X, Y) { ... }
I don`t quite understand what this syntax could do.
The idea is that the compiler would choose the appropriate function
depending on the types of X and Y provided there exists some
compile-time criterium.

For example, given the concrete classes A and B (whereas B is derived
from A) I want to make sure that no matter in what order you provide
arguments to func(), the return value would be always A.


For this example in particular, you can have a look at Boost.TypeTrait s
to detect the relation between two classes.
Jonathan

May 5 '06 #2
The idea is to choose the return type of a template function depending
on the parameters to that function.

I am using boost.type_trai ts already, but what I need is something like
partial specialization for template functions, which is not allowed in
C++. But maybe there are some work-arounds?

May 5 '06 #3
Andrei Tarassov wrote:
The idea is to choose the return type of a template function depending
on the parameters to that function.

I am using boost.type_trai ts already, but what I need is something like
partial specialization for template functions, which is not allowed in
C++. But maybe there are some work-arounds?


Classes, of course. This should get you started:

# include <boost/type_traits.hpp >

class Base {};
class Derived : public Base {};
template <bool b, class A, class B>
struct get_base_second ;

template <class A, class B>
struct get_base_second <true, A, B>
{
typedef A type;
};

template <bool b, class A, class B>
struct get_base_first;

template <class A, class B>
struct get_base_first< true, A, B>
{
typedef A type;
};

template <class A, class B>
struct get_base_first< false, A, B>
{
typedef typename get_base_second <boost::is_base _of<B, A>::value, B,
A>::type type;
};

template <class A, class B>
struct get_base
{
typedef typename get_base_first< boost::is_base_ of<A, B>::value, A,
B>::type type;
};

template <class A, class B>
typename get_base<A, B>::type foo(A& a, B& b)
{
return b; // whatever
}

int main()
{
Base a;
Derived b;

const Base& aa = foo(a, b); // ok
const Derived& bb = foo(a, b); // error
}
Jonathan

May 5 '06 #4
That worked! Thanks!

May 5 '06 #5
Andrei Tarassov wrote:
That worked! Thanks!


It's a pleasure, but again, please quote the message you are answering
to (http://en.wikipedia.org/wiki/Top-posting#Inline_replies)
Jonathan

May 5 '06 #6

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

Similar topics

17
6837
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...
6
3339
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something like that possible? Some workaround? Thank you, Patrick
8
7657
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I specialize Music<Bach>, I expect that the original play() method is available in the specialization, but it is not. How can I fix this? -X
2
5767
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...
5
6571
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>
9
2752
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second assign() function really a specialization of the first assign() or is it an assign() overload? Thank you. -- Marek
4
3345
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
9
3453
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. //------------------------------------------------------------------ // my regular glass class A { }; // my templated class
8
2947
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
1
2217
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
0
7694
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...
0
7609
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...
0
7921
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. ...
0
8118
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...
1
7666
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...
0
7964
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...
0
6278
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...
1
5504
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...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...

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.