Topic: Q: function template and default arguments
Author: Masanori Ohshiro <osilo@vision.tp.chiba-u.ac.jp>
Date: 2000/02/18 Raw View
Thank you, Mr. Nagler for your comments and testing. My compiler is
Metrowerks C++ Pro. 5. I post the results of these cases again :
case 3:
template<class T> int f( T a, T b =3D 1 ); // a declaration from header
template<class T> int f( T a, T b ) {
return a + b;
}
int main() {
f( 1 );
return 0;
}
case 4:
template<class T> int f( T a, T b =3D 1 ); // a declaration from header
template<class T> int f( T a, T b =3D 1 ) {
return a + b;
}
int main() {
f( 1 );
return 0;
}
results:
Metrowerks C++ Pro5, C++Builder4&5FT, Watcom C++, Visual C++ 6
case 3: error ok error error
case 4: ok error ok error
If the rule of default arguments for non-template function can be
applied to template functions, it seems that the compilers have a bug
except for C++Builder. Do all members of committee of C++ standard
agree with this conclusion?
in article Z7mjOAV=tpdLnzM0C=xLSnKkDmmX@4ax.com, Eric Nagler at
epn@eric-nagler.com wrote on 00.2.11 7:03 PM:
>> Thank you and please forgive my poor English.
>
> What poor English? I never saw it.
Thanks!
Masanori Ohshiro
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Masanori Ohshiro <osilo@vision.tp.chiba-u.ac.jp>
Date: 2000/02/07 Raw View
Hi,
I have a question about function template and default arguments. May I
ask about the question?
1) The rule of default arguments says that a default argument shall not be
redefined by a later declaration. Therefore, my compiler's behavior is
as follows:
case 1:
typedef int T;
int f( T a, T b = 1 ); // a declaration from header
int f( T a, T b ) // ok
{
return a + b;
}
case 2:
typedef int T;
int f( T a, T b = 1 ); // a declaration from header
int f( T a, T b = 1 ) // error!
{
return a + b;
}
Sounds good, but...
2) On the other hand, my complier's behavior with function template is as
follows:
case 3:
template<class T> int f( T a, T b = 1 ); // a declaration from header
template<class T> int f( T a, T b )
{
return a + b;
}
int main()
{
f( 1 ); // error! The complier says "function call f( int )
// does not match 'f(T0, T0)'"
return 0;
}
case 4:
template<class T> int f( T a, T b = 1 ); // a declaration from header
template<class T> int f( T a, T b = 1 )
{
return a + b;
}
int main()
{
f( 1 ); // ok!
return 0;
}
I'm confused by these results of declaration for function template. Are
the results valid as standard C++? Or dose my compiler have a bug? Do
you have any idea?
Thank you and please forgive my poor English.
Masanori Oshiro
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Eric Nagler <epn@eric-nagler.com>
Date: 2000/02/11 Raw View
On Mon, 7 Feb 2000 03:30:28 CST, Masanori Ohshiro
<osilo@vision.tp.chiba-u.ac.jp> wrote:
>case 3:
>
> template<class T> int f( T a, T b = 1 ); // a declaration from header
> template<class T> int f( T a, T b )
> {
> return a + b;
> }
>
> int main()
> {
> f( 1 ); // error! The complier says "function call f( int )
> // does not match 'f(T0, T0)'"
> return 0;
> }
Yes, VC++ 6.0 gives the error, but BCB4 and Comeau compile it OK. In
this case it certainly appears to be a Microsoft bug.
> template<class T> int f( T a, T b = 1 ); // a declaration from header
> template<class T> int f( T a, T b = 1 )
> {
> return a + b;
> }
>
> int main()
> {
> f( 1 ); // ok!
> return 0;
> }
In this case all 3 compilers yield an error. Which compiler are you
using?
>Thank you and please forgive my poor English.
What poor English? I never saw it.
EriC++
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]