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

template specification

Just to make it sure:

it is not possibel in C++ to have a template specialization
for more than one type, right?

ie. I have several template type parameters like
string, int, long, char, float, double, complex.

The template for float and double is exactly the same,
the one for the integer types as well.
So I can make the default case for the integer types, but
I have to repeat the code for the floating type specialization, right?

thanks,
marc

Jul 19 '05 #1
4 4101
On Fri, 03 Oct 2003 01:51:55 +0900, Marc Schellens
<m_*********@hotmail.com> wrote:
Just to make it sure:

it is not possibel in C++ to have a template specialization
for more than one type, right?
Right.

ie. I have several template type parameters like
string, int, long, char, float, double, complex.

The template for float and double is exactly the same,
the one for the integer types as well.
So I can make the default case for the integer types, but
I have to repeat the code for the floating type specialization, right?


Well, there are lots of ways you could share the code rather than
repeat it. Inheritence is one way. e.g.

template <class T>
class MyClassFloat
{
//...
};

template<>
class MyClass<float>: public FloatImpl<float> {};
template<>
class MyClass<double>: public FloatImpl<double> {};

Another way is to use indirection to the implementation through
traits, and bind float and double to the same traits.

Tom
Jul 19 '05 #2
> On Fri, 03 Oct 2003 01:51:55 +0900, Marc Schellens
<m_*********@hotmail.com> wrote:

Just to make it sure:

it is not possible in C++ to have a template specialization
for more than one type, right?

Right.

ie. I have several template type parameters like
string, int, long, char, float, double, complex.

The template for float and double is exactly the same,
the one for the integer types as well.
So I can make the default case for the integer types, but
I have to repeat the code for the floating type specialization, right?

Well, there are lots of ways you could share the code rather than
repeat it. Inheritence is one way. e.g.

template <class T>
class MyClassFloat
{
//...
};

template<>
class MyClass<float>: public FloatImpl<float> {};
template<>
class MyClass<double>: public FloatImpl<double> {};

Another way is to use indirection to the implementation through
traits, and bind float and double to the same traits.

Tom


Thanks Tom,

but I was not specific enough, sorry:
I meant a member function specialization.

Any suggestions for that?
thanks,
marc

Jul 19 '05 #3
On Fri, 03 Oct 2003 19:09:10 +0900, Marc Schellens
<m_*********@hotmail.com> wrote:
but I was not specific enough, sorry:
I meant a member function specialization.

Any suggestions for that?


template<class T, bool floatimpl, bool otherimpl>
struct memfunimplchooser
{
static void run(T t)
{
//default impl
}
}

template <class T>
struct memfunimplchooser<T, true, false>
{
static void run(T t)
{
//fp impl
}
};

//etc. other impls.
//and the actual function
template <class T>
void memfun(T t)
{
memfunimplchooser<T, is_float<T>::value,
is_something_else<T>::value>::run(t);
}

Tom
Jul 19 '05 #4
tom_usenet wrote:
On Fri, 03 Oct 2003 19:09:10 +0900, Marc Schellens
<m_*********@hotmail.com> wrote:

but I was not specific enough, sorry:
I meant a member function specialization.

Any suggestions for that?

template<class T, bool floatimpl, bool otherimpl>
struct memfunimplchooser
{
static void run(T t)
{
//default impl
}
}

template <class T>
struct memfunimplchooser<T, true, false>
{
static void run(T t)
{
//fp impl
}
};

//etc. other impls.
//and the actual function
template <class T>
void memfun(T t)
{
memfunimplchooser<T, is_float<T>::value,
is_something_else<T>::value>::run(t);
}

Tom


A bit ugly I think.
But thank you very much for the suggestion,
marc
Jul 19 '05 #5

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

Similar topics

1
by: Joachim Spoerhase | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I am a XSLT-beginner and i read the XSLT-recommendation of the W3C through. But I did'nt really understand section 5.5 of the latest...
5
by: Suzanne Vogel | last post by:
Is it possible to store a pointer to a template function? eg, template<class T> fooFunc() {...} fptr = fooFunc; // <-- I couldn't find any info here, not even in the forums:...
5
by: Brandon Mitchell | last post by:
I have read that it is impossible to define a templated class outside of the specification file, due to a limitation in gcc's linker. I'm using gcc 3.2.3 (no switches) and would like to seperate...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
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...
3
by: IR | last post by:
Hi, I've been trying to do the following (which doesn't compile) : template<class T, class F = Example<T struct Example { F foo(); };
35
by: Steven T. Hatton | last post by:
Perhaps I'm just a bit frustrated, and I will soon realize the clear truth of the matter, but right now I have some serious misgivings about the value of investing a lot of time and effort into...
9
by: vilarneto | last post by:
Hello everyone, Today I started to use template specializations in a project and suddenly faced a curious problem. Following is a complete example that shows the situation: ---------- ...
10
by: wangjk | last post by:
Hi, I know there are many experienced C++ experts be here, i have a puzzle : (1) template <typename L, typename R, bool rL = false, bool rR = flase class A{.......}; (2) template ...
1
by: Ed | last post by:
Hi, guys, I declare a template method in one template class in one library. Compiling is OK, but link is not OK. Header File is: <code> template <typename P = float> class TESTLIB_API Linear...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.