Full specialization of a member function template of a class template

D

Dave

Hello all,

I am trying to create a full specialization of a member function template of
a class template. I get the following errors:

Line 29: 'foo<T1>::bar' : illegal use of explicit template arguments
Line 29: 'bar' : unable to match function definition to an existing
declaration

What am I doing wrong?

Thanks,
Dave


#include <iostream>

using namespace std;

template <typename T1>
class foo
{
public:
template <typename T2>
void bar(const T2 &param);
};

template <typename T1>
template <typename T2>
void foo<T1>::bar(const T2 &param)
{
static_cast<void>(param);

cout << "Point 1" << endl;
}

template <typename T1>
template <>
void foo<T1>::bar<double>(const double &param)
{
static_cast<void>(param);

cout << "Point 2" << endl;
}

int main()
{
foo<int> var;

var.bar(4.5);
}
 
V

Victor Bazarov

Dave said:
Hello all,

I am trying to create a full specialization of a member function template of
a class template. I get the following errors:

Line 29: 'foo<T1>::bar' : illegal use of explicit template arguments
Line 29: 'bar' : unable to match function definition to an existing
declaration

What am I doing wrong?

It is expressly prohibited. If you want to explicitly specialise
a member template, the enclosing class has to be specialised as well.

See 14.7.3/17.
 
J

John Carson

Victor Bazarov said:
It is expressly prohibited. If you want to explicitly specialise
a member template, the enclosing class has to be specialised as well.

See 14.7.3/17.

In this case a workaround is to add the specialised member function to the
class declaration:

template <typename T1>
class foo
{
public:
template <typename T2>
void bar(const T2 &param);

void bar(const double &param);
};

template <typename T1>
void foo<T1>::bar(const double &param)
{
static_cast<void>(param);
cout << "Point 2" << endl;
}
 
B

Buster

Dave said:
Hello all,

I am trying to create a full specialization of a member function template of
a class template.

From the standard (towards the end of 14.7.3),

"In an explicit specialization declaration for a member of a class
template or a member template that appears in namespace scope, the
member template and some of its enclosing class templates may remain
unspecialized, except that the declaration shall not explicitly
specialize a class member template if its enclosing class templates
are not explicitly specialized as well."

Crystal clear?

You can't explicitly specialize a member template of a class template,
but you can explicitly specialize a member template of an explicit
specialization of a class template.
I get the following errors:
Line 29: 'foo<T1>::bar' : illegal use of explicit template arguments
Line 29: 'bar' : unable to match function definition to an existing
declaration

That's pretty crap. G++ 3.3.1 gives "enclosing class templates are not
explicitly specialized".
 
Joined
Jun 4, 2010
Messages
1
Reaction score
0
would you know how to explicitly specialize a member template when template argument for the member is different than template argument of the class?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top