473,385 Members | 2,028 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,385 software developers and data experts.

Specialization of member function template in template class?

Hi,

What is the correct syntax to get the bar<T>::f<int, unsigned>()
function to compile in the following fragment?

Thanks,
Joseph
class foo {
template<typename A, typename B> void f();
};

template<typename T>
class bar {
template<typename A, typename B> void f();
};

template<typename A, typename B>
void foo::f() { }

template<>
void foo::f<int, unsigned>() { }
template <typename T>
template<typename A, typename B>
void bar<T>::f() { }

template <typename T>
template<>
void bar<T>::f<int, unsigned>() { }

Mar 19 '06 #1
4 3283
i also do not know how to writer this functions
but this way is simple:
template<typename T>
class bar {
template<typename A, typename B>
void f(){}
template<>
f<int, unsigned>() { }
};
V

Mar 20 '06 #2
XH*****@gmail.com wrote:
i also do not know how to writer this functions
but this way is simple:
I know this method too.
However, I have a bunch of these functions and it would be unwieldly to
put them in the class declaration.
Hence, I would like to know the syntax for having the implementation
outside.

Joseph

template<typename T>
class bar {
template<typename A, typename B>
void f(){}
template<>
f<int, unsigned>() { }
};
V


Mar 20 '06 #3

Joseph Turian wrote:
Hi,

What is the correct syntax to get the bar<T>::f<int, unsigned>()
function to compile in the following fragment?

Thanks,
Joseph
class foo {
template<typename A, typename B> void f();
};

template<typename T>
class bar {
template<typename A, typename B> void f();
};

template<typename A, typename B>
void foo::f() { }

template<>
void foo::f<int, unsigned>() { }
template <typename T>
template<typename A, typename B>
void bar<T>::f() { }

template <typename T>
template<>
void bar<T>::f<int, unsigned>() { }


That syntax looks good to me, but the Comeau compiler gives the
following error:
a template declaration containing a template parameter list
may not
be followed by an explicit specialization declaration
template<>
^

It seems like it's indicating that the standard doesn't allow this out
side of the class declaration.
IMHO, it's far easier to read template code that includes the
implementation inside the class declaration.

IMO, template methods located outside the class declaration looks
unwieldy, and makes for problematic maintenance.

Mar 20 '06 #4
"Joseph Turian" <tu****@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com
XH*****@gmail.com wrote:
i also do not know how to writer this functions
but this way is simple:


I know this method too.
However, I have a bunch of these functions and it would be unwieldly
to put them in the class declaration.
Hence, I would like to know the syntax for having the implementation
outside.

Joseph

template<typename T>
class bar {
template<typename A, typename B>
void f(){}
template<>
f<int, unsigned>() { }
};
V


VC++ allows you to do this for some backward compatibility reason, but it is
non-standard. According to the standard, you can only specialize member
templates at namespace scope, i.e., outside the class declaration.

Further, you can only specialize member templates if the enclosing class is
fully specialized. What you are trying to do --- specialize a member
template while leaving the enclosing class un-specialized --- is simply not
allowed by the standard.

Relevant sections from the standard are as follows:

Section 14.7.3/18:

"In an explicit specialization declaration for a member of a class template
or a member template that appears in namespace scope, the member template
and some of its enclosing class templates may remain unspecialized, except
that the declaration shall not explicitly specialize a class member template
if its enclosing class templates are not explicitly specialized as well."

You may note that this refers to specializations that appear in namespace
scope. Section 14.7.3/2 makes it clear that this is the only option:

"An explicit specialization shall be declared in the namespace of which the
template is a member, or, for member templates, in the namespace of which
the enclosing class or enclosing class template is a member."

--
John Carson

Mar 20 '06 #5

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...
3
by: Dave | last post by:
Hello all, I am trying to create a full specialization of a member function template of a class template. I get the following errors: Line 29: 'foo<T1>::bar' : illegal use of explicit...
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>
6
by: Kalle Rutanen | last post by:
Here is a short code. I can't see why the template specialization does not work (vsnet2003)? Does the standard allow these kind of specializations? template <int i> class B {}; template...
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
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...
10
by: jason.cipriani | last post by:
I never seem to be able to get this right. Here I have some code: template <typename T, int Nclass A { void f (T); }; template <typename Tvoid A<T,1>::f (T) { } template <typename Tvoid...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.