472,799 Members | 1,751 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,799 software developers and data experts.

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 9357
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
8
by: Massimiliano Alberti | last post by:
Can I specialize a template function in a subclass without overriding it? (the main template function is defined in a base class). Now I'm doing something like that: (in base class)...
13
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
2
by: Thomas Kowalski | last post by:
Hi, I would like to write a template class Polygon<VertexTypthere vertex typ can be eigther a pointer or a value typ. It has an attribute: std::vector<VertexTypvertices; And a methode:...
8
by: mattias.nissler | last post by:
Hi! Here is a problem I ran into at work. The following example doesn't compile on gcc-4.1: struct cons_end {}; template<typename U,typename Vstruct cons { U elem; V tail;
13
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm...
5
by: huili80 | last post by:
For example, like in the following, the part commented out was intended as partial spectialzation, but it would even compile. Is it even legal to partially specialize a nested template class...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.