Sign in to follow this  

template specialization problems [closed]

This topic is 4557 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Im having some template specialization problems, I understand whats happening (I think) but id like a solution to the problem. If I have the class foo: //foo.h template<class T> class foo { public: static T f(T a); }; template<class T> T foo<T>::f(T a) { //do stuff return a; } Id like to specialize the function f for class A: //foo.cpp template<> A foo<A>::f(A a) { //do other stuff return a; } Then in main call: float temp = foo<float>::f(0.2f) A input = 0.2; A temp2 = foo<A>::f(input) This cant be done as the linking of foo<A> creates multiply defined symbols as the non specialized function equates to the specialized one. So how can I define the catch all non specialized function and the specific specializations I need? [Edited by - Structure on June 24, 2005 9:59:45 AM]

Share this post


Link to post
Share on other sites
The quick fix is to mark your specialized function as inline.

Here is what happens. A explicit template specialization must adhere to the one defintion rule. so if you put your explicit specialization in a header file it gets defined into multiple object files as you compile the project. Which is what you saw.

So as I say a quick fix it to mark the specialization as inline, but this is not a great fix in case where you don't want it to be an inline function. For that case, just forward declare the specialization and then place the actual definition of the specialization in a single .cpp file to ensure it is only defined one time.

Share this post


Link to post
Share on other sites
snk_kid, explicit specialization are not actually template anymore so they must be either inline or put into a cpp file. The export keyword is not required in this case. Just imagine the explicit specialization as being no different than a normal function. If you do place it into a cpp file it needs to be declared in a header file as you do a normal non inline function as well.

Share this post


Link to post
Share on other sites
Ahhh, I lov gamedev …….


@ snk_kid

Thanks for the reply , but duke was right in this case..


@duke

Thanks!!!! That was driving me nuts.

The correct class became:

//foo.h

template<class T>
class foo
{
public:

static T f(T a);

};



template<class T>
T foo<T>::f(T a)
{
//do stuff
return a;
}

template<>
A foo<A>::f(A a); // ------------wooo!


//foo.cpp

template<>
A foo<A>::f(A a)
{
//do other stuff
return a;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by duke
snk_kid, explicit specialization are not actually template anymore so they must be either inline or put into a cpp file. The export keyword is not required in this case. Just imagine the explicit specialization as being no different than a normal function. If you do place it into a cpp file it needs to be declared in a header file as you do a normal non inline function as well.


[headshake] i'm aware of that thanks...


@Structure if you didn't get what he was saying he meant:

foo.h

struct A; // forwared declaration

template < typename Tp >
struct foo {

const Tp& bar(const Tp&);
};

template < template Tp >
const Tp& foo<Tp>::bar(const Tp&) { /* most general */ }

template <>
const A& foo<A>::bar(const A&);


A.h

struct A { /* ... */ };



foo.cpp

#include "foo.h"
#include "A.h"

const A& foo<A>::bar(const A&) { /* specialized for A */ }

Share this post


Link to post
Share on other sites

This topic is 4557 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this