[Mingw-users] Explicit function template specialisation - strange results From: John Brown - 2007-11-25 17:19 ```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: 06-RangeSum.cpp:20: error: default argument specified in explicit specializ= ation 06-RangeSum.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= ```

[Mingw-users] Explicit function template specialisation - strange results John Brown <johnbrown105@ho...>
 Re: [Mingw-users] =?utf-8?q?Explicit_function_template_specialisation?= =?utf-8?q?_-_strange=09results?= From: John Brown - 2007-11-25 17:37 ```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 /* 02 */ #include /* 03 */ #include /* 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 /* 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: 06-RangeSum.cpp:20: error: default argument specified in explicit specialization 06-RangeSum.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 ..., 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? ```

 Re: [Mingw-users] Explicit function template specialisation - strange results From: Tuomo Latto - 2007-11-25 17:46 ```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/en-us/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. ```

 Re: [Mingw-users] Explicit function template specialisation - strange results From: Roumen Petrov - 2007-11-25 20:44 ```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 > /* 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: > 06-RangeSum.cpp:20: error: default argument specified in explicit specialization > > 06-RangeSum.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 ```

 Re: [Mingw-users] Explicit function template specialisation - strange results From: John Brown - 2007-11-25 22:17 ```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= ```

 Re: [Mingw-users] Explicit function template specialisation - strange results From: Greg Chicares - 2007-11-26 04:45 ```On 2007-11-25 17:36Z, John Brown wrote: [...] > /* 09 */ template > /* 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: > 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" > My questions: > > 1) I understand that when you write an explicit specialisation, you are supposed > to write 'template<> ...' and not, for example 'template ..., 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 /* 29 */ template 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. And why not just use std::accumulate() instead of writing this function template at all? ```

 Re: [Mingw-users] Explicit function template specialisation - strange results From: John Brown - 2007-11-26 05:43 ```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 ..., 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 > /* 29 */ template 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. 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. ```

 Re: [Mingw-users] Explicit function template specialisation - strange results From: Earnie Boyd - 2007-11-26 13:28 ```Quoting John Brown : > 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 ```

 Re: [Mingw-users] Explicit function template specialisation - strange results From: John Brown - 2007-11-26 14:13 ```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= ```

 Re: [Mingw-users] Explicit function template specialisation - strange results From: Keith Marshall - 2007-11-26 19:54 ```On Sun, 2007-11-25 at 22:44 +0200, Roumen Petrov wrote: > John Brown wrote: > > [SNIP] > > /* 09 */ template > > /* 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: > > 06-RangeSum.cpp:20: error: default argument specified in explicit specialization > > > > 06-RangeSum.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. ```