On Mon, 26 Nov 2007 04:45:38 +0000, Greg Chicares wrote:
> On 2007-11-25 17:36Z, John Brown wrote:
> What follows is an explicit specialization, not an explicit
> instantiation. These would be explicit instantiations:
> template int range_sum(int a, int beg, int end, int initial);
> template double range_sum(double a, int beg, int end, double initial);
OK. Looking back at the chapter, I see that I did not, in fact, do what I
was asked to do.
> > When I compile this, I get:
> > 06-RangeSum.cpp:20: error: default argument specified in explicit
> > specialization
> I believe the operative rule is 14.7.3/21:
> "Default function arguments shall not be specified in [...]
> the explicit specialization of a function template"
OK. I suppose the book can't state *all* the rules, or maybe I missed it.
> > My questions:
> > 1) I understand that when you write an explicit specialisation, you are
> > supposed
> > to write 'template<> ...' and not, for example 'template<int> ..., but I
> > tried it. That is, I changed lines 19 and 29 to 'template' and 'template
> > respectively. The error at line 20 went away, but the error at line 30
> > remained. The error also goes away if I set T = char. However, double and
> > float give the 'default argument' error. Why is this?
> I'm guessing that "hotmail" altered that text, and that you
> actually changed those lines to look like this:
> /* 19 */ template<int>
> /* 29 */ template<double>
Oops. I posted again via Gmane, but I missed those.
> Those would define new and different function templates, with
> non-type arguments. The first is syntactically valid, but it
> almost certainly doesn't do what you want. The second isn't
> valid because a non-type argument can't be a double [14.3.2/1],
> and would have given a different error message, e.g.
> `double' is not a valid type for a template constant parameter
> at least if you got rid of the default argument.
> > 2) If I do not provide any explicit specialisations, the program will
> > compile and produce the expected output:
> > 12
> > 12.00
> > So why does it complain when I provide them?
> Just because the default arguments were respecified.
> But what are you trying to accomplish here?
> Why define partial specializations that just repeat the generic
> code, plugging in a particular type? The point of C++'s template
> facility is that the compiler does that for you.
Indeed, but I was just working exercises from a book. Question 5
says 'write a template function that ... returns the sum ...' and question
6 says, 'Repeat the previous exercise but use explicit instantiation to
manually create specializations for int and double, following the
technique explained in this chapter.' Thanks to your explanation, I now
realise that I did not 'follow the technique explained in this chapter'.
> And why not just use std::accumulate() instead of writing this
> function template at all?
See above. Anyway, std::accumulate has not yet been revealed unto
me. Maybe it will be in the next chapter ("Generic Algorithms").
Thanks for clearing everything up.