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? 7 9122 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 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
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
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
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
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?
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?
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
|
13 posts
views
Thread by Walt Karas |
last post: by
|
5 posts
views
Thread by Levent |
last post: by
|
9 posts
views
Thread by Marek Vondrak |
last post: by
|
2 posts
views
Thread by Thomas Kowalski |
last post: by
|
8 posts
views
Thread by mattias.nissler |
last post: by
|
13 posts
views
Thread by mike b |
last post: by
|
5 posts
views
Thread by huili80 |
last post: by
| | | | | | | | | | |