template function specialization inside template class possible?

Q

quarup

I want to specialize a template function that lives inside a class, but
am getting a compile error in VS.net 2003. Here's my code:

template <class T>
class A {
public:
template <class U>
void f() const;
};

template <class T>
template <>
void A<T>::f<int>() const {}

I get the following errors:
c:\programming\testCPP\testCPP.cpp(19) : error C2768: 'A<T>::f' :
illegal use of explicit template arguments
c:\programming\testCPP\testCPP.cpp(19) : error C2768: 'A<T>::f' :
illegal use of explicit template arguments
c:\programming\testCPP\testCPP.cpp(19) : error C2244: 'f' : unable to
match function definition to an existing declaration
definition
'void A<T>::f<int>(void) const'
existing declarations
'void A<T>::f(void) const'


For comparison, here are two situations that DO work:

1. non-specialized template function inside template class works:

template <class T>
class B {
public:
template <class U>
void f() const;
};

template <class T>
template <class U>
void B<T>::f() const {}

2. specialized template function inside non-template class works:

class C {
public:
template <class U>
void f() const;
};

template <>
void C::f<int>() const {}



Does anyone have any insights? Am I not confirming to the standard, is
my syntax incorrect, or is VS.net broken?
 
V

Victor Bazarov

I want to specialize a template function that lives inside a class,
but am getting a compile error in VS.net 2003. Here's my code:

template <class T>
class A {
public:
template <class U>
void f() const;
};

template <class T>
template <>
void A<T>::f<int>() const {}

This is not allowed. To specialise a member template of a class
template you need to first specialise the class template.
[..]

For comparison, here are two situations that DO work:

1. non-specialized template function inside template class works:

template <class T>
class B {
public:
template <class U>
void f() const;
};

template <class T>
template <class U>
void B<T>::f() const {}

2. specialized template function inside non-template class works:

class C {
public:
template <class U>
void f() const;
};

template <>
void C::f<int>() const {}



Does anyone have any insights? Am I not confirming to the standard, is
my syntax incorrect, or is VS.net broken?

VS.net is broken in some places but not in this one.

V
 
A

Alan Johnson

I want to specialize a template function that lives inside a class, but
am getting a compile error in VS.net 2003. Here's my code:

template <class T>
class A {
public:
template <class U>
void f() const;
};

template <class T>
template <>
void A<T>::f<int>() const {}

I get the following errors:
c:\programming\testCPP\testCPP.cpp(19) : error C2768: 'A<T>::f' :
illegal use of explicit template arguments
c:\programming\testCPP\testCPP.cpp(19) : error C2768: 'A<T>::f' :
illegal use of explicit template arguments
c:\programming\testCPP\testCPP.cpp(19) : error C2244: 'f' : unable to
match function definition to an existing declaration
definition
'void A<T>::f<int>(void) const'
existing declarations
'void A<T>::f(void) const'


For comparison, here are two situations that DO work:

1. non-specialized template function inside template class works:

template <class T>
class B {
public:
template <class U>
void f() const;
};

template <class T>
template <class U>
void B<T>::f() const {}

2. specialized template function inside non-template class works:

class C {
public:
template <class U>
void f() const;
};

template <>
void C::f<int>() const {}



Does anyone have any insights? Am I not confirming to the standard, is
my syntax incorrect, or is VS.net broken?

You can't specialize a template unless all enclosing templates are also
specialized. That is, what you are trying to do isn't allowed by the
standard.

A workaround to achieve pretty much the same thing is to create a proxy
class (or function) to do what you want, and specialize that. For
example:

#include <iostream>

class proxy
{
public:
template <class T>
static void f(T t)
{
std::cout << t << std::endl ;
}

} ;

template <>
void proxy::f<int>(int t)
{
std::cout << "(int)" << t << std::endl ;
}

template <class T>
class A
{
public:
template <class U>
void f(U u)
{
proxy::f<U>(u) ;
}
} ;
 
V

Victor Bazarov

Alan said:
You can't specialize a template unless all enclosing templates are
also specialized. That is, what you are trying to do isn't allowed
by the standard.

A workaround to achieve pretty much the same thing is to create a
proxy class (or function) to do what you want, and specialize that.
For example:

#include <iostream>

class proxy
{
public:
template <class T>
static void f(T t)
{
std::cout << t << std::endl ;
}

} ;

template <>
void proxy::f<int>(int t)
{
std::cout << "(int)" << t << std::endl ;
}

template <class T>
class A
{
public:
template <class U>
void f(U u)
{
proxy::f<U>(u) ;
}
} ;

Actually, if the purpose is to provide a function that takes the
argument of type 'int' (or something like that), a simple
overloading will do:

template <class T>
class A
{
public:
template <class U>
void f(U u)
{
// generic ipmlementation
}
void f(int i)
{
// int-specific implementation
}
};

V
 
Q

quarup

Actually, if the purpose is to provide a function that takes the
argument of type 'int' (or something like that), a simple
overloading will do:

You're right! Thanks, that looks even cleaner. I'll also mentally store
the proxy solution in my bag of tricks.
 
K

k04jg02

Alan said:
A workaround to achieve pretty much the same thing is to create a proxy
class (or function) to do what you want, and specialize that. For
example:

Why would you use a class at all here? It's not obvious to me how it
provides an advantage over simply having templated functions that your
member functions call. Maybe if you friend'd the proxy class and passed
the this pointer to its member functions, so you could write code
similar to having the member functions in the original class?
 
Q

quarup

It's not obvious to me how it
provides an advantage over simply having templated functions that your
member functions call.

You're right. The problem is that you can't have specialized templated
functions inside non-fully specialized template classes (apparently not
part of the standard), which is the reason the thread was started. The
proxy is just a workaround.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top