473,320 Members | 1,872 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,320 software developers and data experts.

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 2076
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.TypeTraits
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_traits 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_traits 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
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...
6
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...
8
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...
2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
5
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
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...
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
9
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. ...
8
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
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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...
0
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....

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.