C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template members of template class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Geo
Guest





PostPosted: Tue Jul 11, 2006 4:23 pm    Post subject: Template members of template class Reply with quote



Having spent several hours trying to get the following code to compile
in GCC and failing

namespace test
{

template<class U, class V=U&> class Formatter__
{
private:
U ss;
public:

Formatter__(V out) : ss(out)
{
}

template<typename T> Formatter__& operator<<(const T &item)
{
ss << item;
return *this;
}

template<> Formatter__& operator<<(const char* s)
{
ss << do_stuff(s);
return *this;
}
};
};

I eventually discovered that it was illegal in standard C++.

My question is, Why ?

I'm not to bothered with the fact that the template specialization
should be at namespace scope, but why can't I specialize operator<<
without specializing Formatter__. What problem does preventing this
solve ?

I can find lots of posts saying it is illegal, but none that explains
why ?

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Greg Herlihy
Guest





PostPosted: Wed Jul 12, 2006 4:18 pm    Post subject: Re: Template members of template class Reply with quote



Geo wrote:
Quote:
Having spent several hours trying to get the following code to compile
in GCC and failing

namespace test
{

template<class U, class V=U&> class Formatter__
{
private:
U ss;
public:

Formatter__(V out) : ss(out)
{
}

template<typename T> Formatter__& operator<<(const T &item)
{
ss << item;
return *this;
}

template<> Formatter__& operator<<(const char* s)
{
ss << do_stuff(s);
return *this;
}
};
};

I eventually discovered that it was illegal in standard C++.

My question is, Why ?

A (full) specialization of a template defines a single, unique
instantation of a general template for a specific set of parameterized
types. So in order to specialize a template member function of a class
template in full, the parameterized types for both the class template
and the member function must be supplied in order to arrive at a
single, unambiguous instantiation of the member function template.

Quote:
I'm not to bothered with the fact that the template specialization
should be at namespace scope, but why can't I specialize operator
without specializing Formatter__. What problem does preventing this
solve ?

A better question would be what problem would fully specialized member
functions for unspecialized class templates solve? It is easy to
mistake specializing a function template with overloading a function -
but because specializations of a template function are considered only
after a function has been already selected when resolving a function
call, they are not the same. In fact the existence of a specialization
for a template function does not improve its chances of being called
for a specific set of parameter types - an outcome that is often as
surprising as it is unexpected. Therefore the change to make to the
example code above (to have it work as expected) - would be to overload
the operator<< member function instead of trying to specialize it:

Formatter__& operator<<(const char* s)
{
ss << do_stuff(s);
return *this;
}

Greg

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.