Jump to content
  • Advertisement
Sign in to follow this  
kimryo

About partially specialize a templated class' member method.

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

Is it possible to partially specialize a templated class' member method by only write the partially specialized version of the method and without rewrite the partially specilaized version of the whole class? Rewrite the whole class cause bad taste of my code...sigh. Any indication is appreciate.Thanks in advance. [Edited by - kimryo on July 17, 2008 2:22:16 AM]

Share this post


Link to post
Share on other sites
Advertisement
if i understood correct, this is what you need
class A
{
template <class T>
void foo(T val);

template <>
void foo<int>(int val)
{
cout<<"this is int";
}

template <>
void foo<bool>(bool val)
{
cout<<"this is bool";
}

void some_other_function()
{

}
};

Share this post


Link to post
Share on other sites
Quote:
Original post by itten
if i understood correct, this is what you need
class A
{
template <class T>
void foo(T val);

template <>
void foo<int>(int val)
{
cout<<"this is int";
}

template <>
void foo<bool>(bool val)
{
cout<<"this is bool";
}

void some_other_function()
{

}
};


Thanks for reply, but this is fully specialize of a templated method of an untemplated class.
Actually what I need is something like this

template<typename T1,typename T2>
class A
{
void foo(T2 val);

void some_other_function();
void And_many_many_other_functions1();
//...
void And_many_many_other_functionsn();

};

template <typename T1,typename T2>
void A<T1,T2>::foo(T2 val)
{
cout<<"this is all templated";
}

template <typename T1>
void A<T1,int>::foo(int val)
{
cout<<"this is partly int";
}

template <typename T1>
void A<T1,bool>::foo(bool val)
{
cout<<"this is partly bool";
}

I wrote like this, but my MSC compiler said it is illegal to do so, declaration and definition mismatch.
Since partially specialized function is also illegal in C++, I can not figure out a way to done like that.

Share this post


Link to post
Share on other sites
Gcc (v3.4.4) seams able to handle it, and visual studio 8:


#include <iostream>

using std::cout;
using std::endl;

template<class T>
class Test {
public:
void doit() { cout << "General doit!" << endl; }
};

template<>
void Test<int>::doit() { cout << "Specialized doit!" << endl; }

int main() {
Test<char> a;
Test<int> b;
a.doit();
b.doit();
return 0;
}

Share this post


Link to post
Share on other sites
i got it. Sorry, as far as i know, there is no way to do like you want. I tried your example in VS2005 and got the error C2244. In its description i found an axample whitch is exactly the same as yours. Here it is:
"
You cannot partially specialize a function template.

Copy Code
// C2244d.cpp
template<class T, class U>
class QRS {
void func(T t, U u);
};

template<class T>
void QRS<T,int>::func(T t, int u) {} // C2244
"

Share this post


Link to post
Share on other sites
mzeo77: That's still complete, not partial, specialization.

kimryo: Functions (member or otherwise) can't be partially specialized.

Share this post


Link to post
Share on other sites
Quote:
Original post by mzeo77
Gcc (v3.4.4) seams able to handle it, and visual studio 8:


#include <iostream>

using std::cout;
using std::endl;

template<class T>
class Test {
public:
void doit() { cout << "General doit!" << endl; }
};

template<>
void Test<int>::doit() { cout << "Specialized doit!" << endl; }

int main() {
Test<char> a;
Test<int> b;
a.doit();
b.doit();
return 0;
}


For fully specialized, of course can do so. Try make two template parameters, and partially specialize one of the two.

Share this post


Link to post
Share on other sites
Thanks buddies. My design failed, and subject to change again...

When these features will be implemented, like partially specialize of function or templated class' member method...
Just feels like drawbacks of C++ and make program looks ugly...

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!