Jump to content
  • Advertisement
Sign in to follow this  
GorbGorb

template specialization problem

This topic is 2626 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

Hello,

I've a problem with a specialization of template member of a template class. Here is what I want to do:



template<class X>
class A
{
template<class Y>
void func();
template<>
void func<X>();
};



This code compiles (and works) with Visual C++, but according to the standard, it's wrong: If a template member of a template class is specialized, it has to be done in the namespace the class was defined. I tried to work around that, but I can't figure out how:
I tried

template<class X>
class A
{
template<class Y>
void func();
};
template<> template<class Y>
void A<Y>::func<Y>();

and

template<class X>
class A
{
template<class Y>
void func();
};
template<class X> template<>
void A<X>::func<X>();

, but both versions don't work.

Does anybody have a solution for what I want to achieve?

Share this post


Link to post
Share on other sites
Advertisement
If I'm not mistaken, you can't actually specialize the member template unless you specialize the class itself first:

[source lang="cpp"]


namespace foo {

template<class X>
class A
{
public:
template<class Y>
void func();
};

template<class X> template<class Y>
void A<X>::func() {
std::cout << "unspecialized, unspecialized" << std::endl;
}

template<>
class A<int>
{
public:
template<class Y>
void func();
};

template<class Y>
void A<int>::func() {
std::cout << "specialized, unspecialized" << std::endl;
}

template<>
void A<int>::func<int>() {
std::cout << "specialized, specialized" << std::endl;
}

} // foo


int main(int, char*[])
{
using namespace foo;

A<float> af;
af.func<float>();
af.func<int>();

A<int> ai;
ai.func<float>();
ai.func<int>();

// output:
// unspecialized, unspecialized
// unspecialized, unspecialized
// specialized, unspecialized
// specialized, specialized
}

[/source]

Note that you might be able to work around it with type-to-type mapping and overloading, something like the following:

[source lang="cpp"]


namespace foo {

template<class X>
class A
{
public:
template<class Y>
void func() {
foo::func(*this, Loki::Type2Type<Y>());
}
};

template<class X, class Y>
void func(A<X> const& a, Loki::Type2Type<Y>) {
std::cout << "unspecialized, unspecialized" << std::endl;
}

//template<class X>
//void func(A<X> const& a, Loki::Type2Type<int>) {
// std::cout << "unspecialized, specialized" << std::endl;
//}

template<class Y>
void func(A<int> const& a, Loki::Type2Type<Y>) {
std::cout << "specialized, unspecialized" << std::endl;
}

void func(A<int> const& a, Loki::Type2Type<int>) {
std::cout << "specialized, specialized" << std::endl;
}

} // foo

// Loki::Type2Type is just this:
//template<typename T>
//struct Type2Type {
// typedef T OriginalType;
//};

int main(int, char*[])
{
using namespace foo;

A<float> af;
af.func<float>();
af.func<int>();

A<int> ai;
ai.func<float>();
ai.func<int>();

// output:
//unspecialized, unspecialized
//unspecialized, unspecialized
//specialized, unspecialized
//specialized, specialized
}
[/source] Edited by __sprite

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!