Specialization of member function template in template class?

J

Joseph Turian

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>() { }
 
X

XHengDF

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
 
J

Joseph Turian

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
 
A

Axter

Joseph said:
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.
 
J

John Carson

Joseph Turian said:
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

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."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top