Topic: Quirky template code generation, bugs?
Author: gjohnson@netcom.com (Serendipitous Freelance Hacker)
Date: Sat, 7 Jan 1995 15:15:03 GMT Raw View
/*
Using BC++ 4.5 I have run into the following two template code generation
quirks. Because I don't subscribe to the DWP I though I would ask here
before submitting bug reports to Borland.
*/
// Note the template functions default argument t()
template < class t > void f( const t & = t() ) {}
// Note the lack of a default constructor
struct b { b( int ) {} };
int main()
{
#ifdef thisfails // As expected
f(); // Could not find a match for 'f()'
#endif
#ifdef thisfailstoo // As expected
f( 42 );
f( 56.0 );
f(); // Ambiguity between 'f(const double &)' and 'f(const int &)'
#endif
f( 42 );
// Bug or quirk in template code generation rules?
f(); // finds f( const int & ), understandable but surprising
// Bug or is a default constructor required because of the default argument?
f( b( 42 ) ); // Could not find a match for 'b::b()'
return 0;
}
Author: gjohnson@netcom.com (Serendipitous Freelance Hacker)
Date: Sun, 8 Jan 1995 06:59:13 GMT Raw View
The STL uses = t(), it must be there to enforce the availability
of default constructors then . . .
My apologies if the STL docs cover this, I haven't undertaken
round two of the ghostscript vs STL docs wrestling match yet.
Author: chris@double.actrix.gen.nz (Chris Double)
Date: Sun, 8 Jan 95 15:15:06 -0800 Raw View
In <gjohnsonD21Jp3.Kq1@netcom.com> gjohnson@netcom.com (Serendipitous Freelance Hacker) writes:
>/*
>Using BC++ 4.5 I have run into the following two template code generation
>quirks. Because I don't subscribe to the DWP I though I would ask here
>before submitting bug reports to Borland.
>*/
>// Note the template functions default argument t()
>template < class t > void f( const t & = t() ) {}
>// Note the lack of a default constructor
>struct b { b( int ) {} };
>int main()
>{
> f( 42 );
>// Bug or quirk in template code generation rules?
> f(); // finds f( const int & ), understandable but surprising
A function f(const int& = int()) exists. It was created by the f(42) call.
It looks like this is how it should happen.
>// Bug or is a default constructor required because of the default argument?
> f( b( 42 ) ); // Could not find a match for 'b::b()'
If you provide a constructor for a class then the compiler will not generate
a default constructor. The template specifies that a default argument is created
by calling the default constructor. There is no default constructor so it is an
error. I don't think this is a bug, more the way templates work.
Regards,
Chris.