468,491 Members | 2,010 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

template function specialization inside template class possible?

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
7 8981
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
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
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
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
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
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
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.

Similar topics

17 posts views Thread by Paul MG | last post: by
8 posts views Thread by Massimiliano Alberti | last post: by
2 posts views Thread by Thomas Kowalski | last post: by
8 posts views Thread by mattias.nissler | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.