From: Keith Marshall <keithmarshall@us...>  20071126 19:54:23

On Sun, 20071125 at 22:44 +0200, Roumen Petrov wrote: > John Brown wrote: > > [SNIP] > > /* 09 */ template <class T> > > /* 10 */ T range_sum(T a[], int beg, int end, T initial=T()) > > [SNIP] > > /* 18 */ // explicit instantiation > > /* 19 */ template <> > > /* 20 */ int range_sum(int a[], int beg, int end, int initial=int()) > > [SNIP] > > /* 29 */ template <> > > /* 30 */ double range_sum(double a[], int beg, int end, double initial=double()) > > [SNIP] > > When I compile this, I get: > > 06RangeSum.cpp:20: error: default argument specified in explicit specialization > > > > 06RangeSum.cpp:30: error: default argument specified in explicit specialization > > > What about to: > s/int initial=int()/int initial/ > s/double initial=int()/double initial/ > > > And what is related to mingw ? John is using MinGW as his C++ compiler, and it is whingeing about his code. While this may be a more generic C++ problem, it is still a perfectly legitimate discussion topic for this list. Regards, Keith. 
From: John Brown <johnbrown105@ho...>  20071125 17:19:46

Consider the following template function: /* 01 */ #include=20 /* 02 */ #include=20 /* 03 */ #include=20 /* 04 */=20 /* 05 */ // this function is supposed to calculate the sum of /* 06 */ // elements in the range beg  end inclusive /* 07 */ // and an optional initial value /* 08 */=20 /* 09 */ template=20 /* 10 */ T range_sum(T a[], int beg, int end, T initial=3DT()) /* 11 */ { /* 12 */ T sum =3D 0; /* 13 */ for (int i=3Dbeg; i<=3Dend; i++) /* 14 */ sum +=3D a[i]; /* 15 */ return sum + initial; /* 16 */ } /* 17 */=20 /* 18 */ // explicit instantiation /* 19 */ template <> /* 20 */ int range_sum(int a[], int beg, int end, int initial=3Dint()) /* 21 */ { /* 22 */ int sum =3D 0; /* 23 */ for (int i=3Dbeg; i<=3Dend; i++) /* 24 */ sum +=3D a[i]; /* 25 */ return sum + initial; /* 26 */ }; /* 27 */=20 /* 28 */=20 /* 29 */ template <> /* 30 */ double range_sum(double a[], int beg, int end, double initial=3Ddo= uble()) /* 31 */ { /* 32 */ double sum =3D 0; /* 33 */ for (int i=3Dbeg; i<=3Dend; i++) /* 34 */ sum +=3D a[i]; /* 35 */ return sum + initial; /* 36 */ } /* 37 */=20 /* 38 */=20 /* 39 */ int main() /* 40 */ { /* 41 */ int data[] =3D {1,2,3,4,5}; /* 42 */ double data2[] =3D {1.0, 2.0, 3.0, 4.0, 5.0}; /* 43 */=20 /* 44 */ std::cout << range_sum<>(data, 2, 4, int()) << std::endl; /* 45 */ std::cout << std::fixed << std::setprecision(2) << range_sum<>(= data2, 2, 4, double()) << std::endl; /* 46 */=20 /* 47 */ return 0; /* 48 */ } When I compile this, I get: 06RangeSum.cpp:20: error: default argument specified in explicit specializ= ation 06RangeSum.cpp:30: error: default argument specified in explicit specializ= ation My questions: 1) I understand that when you write an explicit specialisation, you are sup= posed to write 'template<> ...' and not, for example 'template ..., but I tri= ed 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 =3D char. However, double and float= give the 'default argument' error. Why is this? 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? _________________________________________________________________ Share life as it happens with the new Windows Live.Download today it's FREE= ! http://www.windowslive.com/share.html?ocid=3DTXT_TAGLM_Wave2_sharelife_1120= 07= 
From: John Brown <johnbrown105@ho...>  20071125 17:37:17

It seems that Hotmail is removing anything between angle brackets. Probably a "security feature". Sigh... here is the post again. Consider the following template function: /* 01 */ #include <iostream> /* 02 */ #include <ostream> /* 03 */ #include <iomanip> /* 04 */ /* 05 */ // this function is supposed to calculate the sum of /* 06 */ // elements in the range beg  end inclusive /* 07 */ // and an optional initial value /* 08 */ /* 09 */ template <class T> /* 10 */ T range_sum(T a[], int beg, int end, T initial=T()) /* 11 */ { /* 12 */ T sum = 0; /* 13 */ for (int i=beg; i<=end; i++) /* 14 */ sum += a[i]; /* 15 */ return sum + initial; /* 16 */ } /* 17 */ /* 18 */ // explicit instantiation /* 19 */ template <> /* 20 */ int range_sum(int a[], int beg, int end, int initial=int()) /* 21 */ { /* 22 */ int sum = 0; /* 23 */ for (int i=beg; i<=end; i++) /* 24 */ sum += a[i]; /* 25 */ return sum + initial; /* 26 */ }; /* 27 */ /* 28 */ /* 29 */ template <> /* 30 */ double range_sum(double a[], int beg, int end, double initial=double()) /* 31 */ { /* 32 */ double sum = 0; /* 33 */ for (int i=beg; i<=end; i++) /* 34 */ sum += a[i]; /* 35 */ return sum + initial; /* 36 */ } /* 37 */ /* 38 */ /* 39 */ int main() /* 40 */ { /* 41 */ int data[] = {1,2,3,4,5}; /* 42 */ double data2[] = {1.0, 2.0, 3.0, 4.0, 5.0}; /* 43 */ /* 44 */ std::cout << range_sum<>(data, 2, 4, int()) << std::endl; /* 45 */ std::cout << std::fixed << std::setprecision(2) /* 46 */ << range_sum<>(data2, 2, 4, double()) << std::endl; /* 47 */ /* 48 */ return 0; /* 49 */ } When I compile this, I get: 06RangeSum.cpp:20: error: default argument specified in explicit specialization 06RangeSum.cpp:30: error: default argument specified in explicit specialization 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? 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? 
From: Greg Chicares <gchicares@sb...>  20071126 04:45:47

On 20071125 17:36Z, John Brown wrote: [...] > /* 09 */ template <class T> > /* 10 */ T range_sum(T a[], int beg, int end, T initial=T()) > /* 11 */ { > /* 12 */ T sum = 0; > /* 13 */ for (int i=beg; i<=end; i++) > /* 14 */ sum += a[i]; > /* 15 */ return sum + initial; > /* 16 */ } > /* 17 */ > /* 18 */ // explicit instantiation 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); > /* 19 */ template <> > /* 20 */ int range_sum(int a[], int beg, int end, int initial=int()) > /* 21 */ { > /* 22 */ int sum = 0; > /* 23 */ for (int i=beg; i<=end; i++) > /* 24 */ sum += a[i]; > /* 25 */ return sum + initial; > /* 26 */ }; Stray semicolon on line 26, BTW. > When I compile this, I get: > 06RangeSum.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" > 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> Those would define new and different function templates, with nontype arguments. The first is syntactically valid, but it almost certainly doesn't do what you want. The second isn't valid because a nontype 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. And why not just use std::accumulate() instead of writing this function template at all? 
From: Tuomo Latto <djv@ik...>  20071125 17:46:54

John Brown wrote: > 2) If I do not provide any explicit specialisations, the program will > compile and produce the expected output: ... > So why does it complain when I provide them? I'd say it's a syntax error in declaration, but I haven't played with templates nearly often enough to be sure. (Maybe you need to explicitly state typename/class?) I wonder if these help: http://www.gotw.ca/publications/mill17.htm http://www.gamedev.net/community/forums/topic.asp?topic_id=399591 http://www.cprogramming.com/tutorial/template_specialization.html http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc16partial_specialization.htm http://msdn2.microsoft.com/enus/library/3967w96f(VS.80).aspx http://www.gotw.ca/gotw/049.htm http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=50&rl=1  Tuomo ... $ make fire make: *** No rule to make target `fire' Stop. $ why? why?: No match. 
From: Roumen Petrov <bugtrack@ro...>  20071125 20:44:53

John Brown wrote: > It seems that Hotmail is removing anything between angle brackets. > Probably a "security feature". Sigh... here is the post again. > > Consider the following template function: > > [SNIP] > /* 09 */ template <class T> > /* 10 */ T range_sum(T a[], int beg, int end, T initial=T()) > [SNIP] > /* 18 */ // explicit instantiation > /* 19 */ template <> > /* 20 */ int range_sum(int a[], int beg, int end, int initial=int()) > [SNIP] > /* 29 */ template <> > /* 30 */ double range_sum(double a[], int beg, int end, double initial=double()) > [SNIP] > When I compile this, I get: > 06RangeSum.cpp:20: error: default argument specified in explicit specialization > > 06RangeSum.cpp:30: error: default argument specified in explicit specialization > What about to: s/int initial=int()/int initial/ s/double initial=int()/double initial/ And what is related to mingw ? Roumen 
From: John Brown <johnbrown105@ho...>  20071125 22:17:07

On Sun, 25 Nov 2007 22:44:31 +0200, Roumen Petrov wrote: > What about to: > s/int initial=3Dint()/int initial/ > s/double initial=3Dint()/double initial/ > The question specifically says that 'initial' should be a default parameter, and I should use the default constructor as the=20 default value. Also, the book explicitly says something that I understand as: 'template T f(T x)' is not legal, but 'template T f(T x=3D)' is legal and he provides his own example of the latter construct. > > And what is related to mingw ? It is related to mingw in the sense that: I think that the author believes that what he is asking for is legal. Sometimes, the point of an exercise is to show that a particular technique will *not* work, but I do not think that that is the goal in this case. However, it does not compile. Further, it can be made to compile if I use int or char, but not if I use float or double, even though I am not performing any operation that is defined for int and char,=20 but not for float and double. In short, the compiler does not work as I expect. Whom but the mingw developers should I ask? _________________________________________________________________ Your smile counts. The more smiles you share, the more we donate.=A0 Join i= n. http://www.windowslive.com/smile?ocid=3DTXT_TAGLM_Wave2_oprsmilewlhmtagline= 
From: Keith Marshall <keithmarshall@us...>  20071126 19:54:23

On Sun, 20071125 at 22:44 +0200, Roumen Petrov wrote: > John Brown wrote: > > [SNIP] > > /* 09 */ template <class T> > > /* 10 */ T range_sum(T a[], int beg, int end, T initial=T()) > > [SNIP] > > /* 18 */ // explicit instantiation > > /* 19 */ template <> > > /* 20 */ int range_sum(int a[], int beg, int end, int initial=int()) > > [SNIP] > > /* 29 */ template <> > > /* 30 */ double range_sum(double a[], int beg, int end, double initial=double()) > > [SNIP] > > When I compile this, I get: > > 06RangeSum.cpp:20: error: default argument specified in explicit specialization > > > > 06RangeSum.cpp:30: error: default argument specified in explicit specialization > > > What about to: > s/int initial=int()/int initial/ > s/double initial=int()/double initial/ > > > And what is related to mingw ? John is using MinGW as his C++ compiler, and it is whingeing about his code. While this may be a more generic C++ problem, it is still a perfectly legitimate discussion topic for this list. Regards, Keith. 
From: John Brown <johnbrown105@ho...>  20071126 05:43:58

On Mon, 26 Nov 2007 04:45:38 +0000, Greg Chicares wrote: > > On 20071125 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: > > 06RangeSum.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 > nontype arguments. The first is syntactically valid, but it > almost certainly doesn't do what you want. The second isn't > valid because a nontype 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. OK. > > > 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. OK. > > 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. 
From: Earnie Boyd <earnie@us...>  20071126 13:28:56

Quoting John Brown <johnbrown105@...>: > It seems that Hotmail is removing anything between angle brackets. > Probably a "security feature". Sigh... here is the post again. > Or the result of xhtml format for the client you use to read the mail. Earnie 
From: John Brown <johnbrown105@ho...>  20071126 14:13:01

Earnie Boyd wrote: >=20 > Quoting John Brown : >=20 >> It seems that Hotmail is removing anything between angle brackets. >> Probably a "security feature". Sigh... here is the post again. >> >=20 > Or the result of xhtml format for the client you use to read the mail. >=20 > Earnie >=20 Hotmail is the culprit. When I look at the message in my "Sent" folder, the "tags" are stripped, and they do not appear in Gmane either. _________________________________________________________________ Put your friends on the big screen with Windows Vista=AE + Windows Live=99. http://www.microsoft.com/windows/shop/specialoffers.mspx?ocid=3DTXT_TAGLM_C= PC_MediaCtr_bigscreen_102007= 