By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,244 Members | 1,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,244 IT Pros & Developers. It's quick & easy.

template function specialization inside template class possible?

P: n/a
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?

Aug 25 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
qu****@gmail.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 25 '06 #2

P: n/a
qu****@gmail.com wrote:
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) ;
}
} ;

--
Alan Johnson

Aug 25 '06 #3

P: n/a
Alan Johnson wrote:
qu****@gmail.com wrote:
>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) ;
}
} ;
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 25 '06 #4

P: n/a
Thanks, guys, that's a huge help. Your proxy is an elegant workaround!

Cheers,
-Q

Alan Johnson wrote:
qu****@gmail.com wrote:
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) ;
}
} ;

--
Alan Johnson
Aug 25 '06 #5

P: n/a
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.

Victor Bazarov wrote:
Alan Johnson wrote:
qu****@gmail.com wrote:
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) ;
}
} ;

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 25 '06 #6

P: n/a
Alan Johnson wrote:
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?

Aug 25 '06 #7

P: n/a
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.

k0*****@gmail.com wrote:
Alan Johnson wrote:
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?
Aug 25 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.