By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,106 Members | 1,403 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,106 IT Pros & Developers. It's quick & easy.

Specialization of member function template in template class?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
"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.