From -5727339850048645341
X-Google-Thread: f78e5,d622c2d05233cea8
X-Google-Attributes: gidf78e5,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news2.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.speakeasy.net!news.speakeasy.net.POSTED!not-for-mail
NNTP-Posting-Date: Wed, 12 Jul 2006 10:20:11 -0500
Return-Path: <devnull@stump.algebra.com>
X-Authentication-Warning: mulga.csse.unimelb.edu.au: fjh set sender to devnull@stump.algebra.com using -f
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Original-To: std-c++@mailman.ucar.edu
Delivered-To: std-c++@mailman.ucar.edu
From: "Greg Herlihy" <greghe@pacbell.net>
Newsgroups: comp.std.c++
Subject: Re: Template members of template class
Organization: http://groups.google.com
Message-ID: <1152687147.444426.240120@s13g2000cwa.googlegroups.com>
References: <1152613761.862951.8940@35g2000cwc.googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
X-Complaints-To: groups-abuse@google.com
User-Agent: G2/0.2
X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418.8 (KHTML, like Gecko) Safari/419.3,gzip(gfe),gzip(gfe)
Complaints-To: groups-abuse@google.com
Injection-Info: s13g2000cwa.googlegroups.com; posting-host=70.231.172.113;
   posting-account=JdllFQ0AAAC-QghphnHMZz5q0GHnzGUJ
X-Greylisting: NO DELAY (Relay+Sender autoqualified);
	processed by UCSD_GL-v2.1 on mailbox7.ucsd.edu;
	Tue, 11 July 2006 23:52:43 -0700 (PDT)
X-Spamscanner: mailbox7.ucsd.edu  (v1.6 Jul  6 2006 14:57:05, 0.0/5.0 3.1.3)
X-MailScanner: PASSED (v1.2.8 13552 k6C6qhcZ014102 mailbox7.ucsd.edu)
X-Virus-Scanned: amavisd-new at ucar.edu
X-Virus-Scanned: amavisd-new at csse.unimelb.edu.au
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
X-Virus-Scanned: amavisd-new at csse.unimelb.edu.au
Date: Wed, 12 Jul 2006 10:18:25 CST
Lines: 73
NNTP-Posting-Host: 65.182.171.162
X-Trace: sv3-FifOpNWbvCNGs0ZO+p0YRb5i5wym/XTQO98z9yoYrH7yJelyMOZVxhvNOSaDUztsmq+khJy1s+AQ9IG!jHragB6TIJwW6by/CDQwL38xzmKmMZ6VOaEe1yNjIFFxuKFre/T07BfGnMc1xHbk9GvE03i/SX0Y!V2iBKAK9/erwPQwkNfoyZL1560g47/F8EyHfpYYwpvc=
X-Complaints-To: abuse@speakeasy.net
X-DMCA-Complaints-To: abuse@speakeasy.net
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.32
Xref: g2news2.google.com comp.std.c++:2687

Geo wrote:
> 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.

> 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                      ]



