• Advertisement
Sign in to follow this  

Template specialization with pointer type (C++)

This topic is 3628 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, scoured the web, did an hour of trial and error, and asked at ##c++ and I still don't know. Suppose I have: template <typename _type> class Foo { public: void bar(_type& e); }; and I wanted to write a specialization of the bar method for template class Foo<AnotherClass*>; how would I do it? I know how to do a specialization of bar for say template class Foo<AnotherClass>; but things seem to really change when a pointer type isused. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Are you suggesting I write a specialization for the class? I only want to specialize the bar method. For Foo<AnotherClass> I know that'd just be:

template <> Foo<AnotherClass>::bar(AnotherClass& e){}

But I want a specialization of bar for Foo<AnotherClass*>. It's not:
template <> Foo<AnotherClass*>::bar((AnotherClass*)& e){}
or
template <> Foo<AnotherClass*>::bar(AnotherClass& e){}
or any of the multitude of function headers I've tried. Do you know?

Share this post


Link to post
Share on other sites
Quote:
Original post by Baiame
Are you suggesting I write a specialization for the class?


This is what it looks like what you want.

If you want to specialize the class _type parameter.

Alternatively, use overloading:
template <typename _type> class Foo
{
public:
void bar(_type& e);
void bar(AnotherClass * e);

// and more....
void bar(int i);

};


Of course, setting _type to any of those overloaded types will result in problems.

Share this post


Link to post
Share on other sites
You can't specialize individual un-templated members of templated classes. If you want to do that, consider having the member function delegate to a static or global function which is templated, then specializing that template. You can do it with overloading, but the rules are so messy that I wouldn't suggest it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
You can't specialize individual un-templated members of templated classes.

Ah right, didn't know that, thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
You can't specialize individual un-templated members of templated classes.
Yes you can. See 14.7.3.16. The following works just fine:
struct baz
{
};

template < typename Type >
class foo
{

public:

void bar(Type & e);

};

template <>
void foo< baz * >::bar(baz * & e)
{
}

int main()
{
baz * quux;
foo< baz * > qux;
qux.bar(quux);
}

Σnigma

Share this post


Link to post
Share on other sites
^ Ah right, I was wondering why specializing bar with AnotherClass (well, the equivalent of that) compiled without errors.

So surely there's a way to do it with a pointer type? EDIT- Of course there is, you posted it, thanks. But although it might be standard C++, I get an "unable to match function definition to an existing declaration" error in the compiler included with MSVS 2008.

Alternatively, I kinda doubt it, but is there a way to extract the type from a pointer type and check for members (at compile time)? I know you can do
#ifdef _type::blah()
but if _type is a pointer type, that's not very useful.

Share this post


Link to post
Share on other sites
Quote:
Original post by Baiame
EDIT- Of course there is, you posted it, thanks. But although it might be standard C++, I get an "unable to match function definition to an existing declaration" error in the compiler included with MSVS 2008.
It works just fine in Visual C++ 2008 Express Edition here. Perhaps your code is subtly different?
Quote:
Alternatively, I kinda doubt it, but is there a way to extract the type from a pointer type and check for members (at compile time)? I know you can do
#ifdef _type::blah()
but if _type is a pointer type, that's not very useful.

You can't do #ifdef type::blah() since types don't exist when preprocessing occurs. You can use some arcane possibly non-standard template wizardry to determine the existance of a member with particular name and prototype, but before doing so you really ought to ask yourself exactly why you want to do so and if there isn't really a better way. For the terminally curious, google SFINAE (Substitution Failure Is Not An Error), but make sure you have a couple of asprin handy first!

Σnigma

Share this post


Link to post
Share on other sites
Quote:
Original post by Enigma
It works just fine in Visual C++ 2008 Express Edition here. Perhaps your code is subtly different?

Still can't find any differences, but they must be there: I'm using the same version. I'll try your code itself.
Quote:
Original post by Enigma
You can't do #ifdef type::blah() since types don't exist when preprocessing occurs.

Didn't think about it thoroughly (only tried it), you're right, it really shouldn't work. But it did seem to work in MSVS2008 Express, guess I should run another test.

I'll just use function pointers to achieve the behaviour I need. Thanks everyone.

Share this post


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

  • Advertisement