[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.
Notices
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#include <iostream>
#include "foo.hpp"
using namespace std;
int main()
{
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;
return 0;
}
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.
Something like that allows you to minimize explicit specialization if the only differences are typedefs and static data.
If you need to do something like that for function specialization, you can do that with static functions, although it can become a bit ugly if you need access to a lot of members in the functions:
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.
Kevin Barry
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.