469,267 Members | 971 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

Full specialization of a member function template of a class template


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);
}
Jul 22 '05 #1
3 1906
"Dave" <be***********@yahoo.com> wrote...

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.

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);
}

Jul 22 '05 #2
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:UfXlc.24994$_41.1789783@attbi_s02
"Dave" <be***********@yahoo.com> wrote...

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.


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;
}
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #3
Dave wrote:
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".

--
Regards,
Buster.
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Paul MG | last post: by
6 posts views Thread by Kalle Rutanen | last post: by
9 posts views Thread by stephen.diverdi | last post: by
10 posts views Thread by jason.cipriani | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.