[SOLVED] Question Regarding C++ Template Specialization.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
using namespace std;
cout << "For <int , int > foo has " << foo<int , int >::str
<< " and " << foo<int , int >::getStr() << endl;
cout << "For <float, float> foo has " << foo<float, float>::str
<< " and " << foo<float, float>::getStr() << endl;
cout << "For <int , float> foo has " << foo<int , float>::str
<< " and " << foo<int , float>::getStr() << endl;
cout << "For <float, int > foo has " << foo<float, int >::str
<< " and " << foo<float, int >::getStr() << endl;
So far so good. All works fine and the output as expected is:
For <int , int > foo has str<T1 , T2 > and getStr<T1 , T2 >
For <float, float> foo has str<float, float> and getStr<float, float>
For <int , float> foo has str<T1 , T2 > and getStr<T1 , T2 >
For <float, int > foo has str<T1 , T2 > and getStr<T1 , T2 >
Now here is where I try to get funky and mess things up.
I want to do a specialization for just one of the two parameters and add the following code into foo.hpp
However if I add this I have duplicated the definition of foo, which as you can see in both the main and the specialization is exactly the same; and now I also have to duplicate the source implementation for the new template specialization. For this example of course it is a simple initialization of str, however in a real program it could had been a lot more source.
I know that I can get thing behaving better if I create helper classes that all depend in one parameter, however I was wondering if I am missing something, or if there are other solutions to the above problem that I have not yet find, or if they have address this in C++0x.
I think the sort of specialization you're talking about can't be done with a single class definition; therefore, you're better off finding a way to confine what's specialized to a class that only contains specialized members.