Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Specialization of member function template in template class?

Reply
Thread Tools

Specialization of member function template in template class?

 
 
Joseph Turian
Guest
Posts: n/a
 
      03-19-2006
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>() { }

 
Reply With Quote
 
 
 
 
XHengDF@gmail.com
Guest
Posts: n/a
 
      03-20-2006
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

 
Reply With Quote
 
 
 
 
Joseph Turian
Guest
Posts: n/a
 
      03-20-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
Axter
Guest
Posts: n/a
 
      03-20-2006

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.

 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      03-20-2006
"Joseph Turian" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com
> (E-Mail Removed) 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Full specialization of a member function template of a class template Dave C++ 4 06-04-2010 12:15 PM
template specialization overriding non-specialization? Joseph Turian C++ 2 04-16-2006 02:46 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Out-of-class-template member template specialization syntax BigMan C++ 1 03-26-2005 02:23 PM
Member template function specialization in a template class Ruben Campos C++ 3 02-15-2005 10:40 AM



Advertisments