469,268 Members | 1,010 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 2748
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Paul MG | last post: by
6 posts views Thread by Kalle Rutanen | last post: by
9 posts views Thread by stephen.diverdi | last post: by
10 posts views Thread by jason.cipriani | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.