473,322 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 9416
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.