Topic: Default argument specified but not used?


Author: Allan_W@my-dejanews.com (Allan W)
Date: Tue, 18 Jun 2002 21:17:59 GMT
Raw View
Daniel Frey <daniel.frey@aixigo.de> wrote
> > >class Null {} null;
> > >
> > >template< typename T, typename U >
> > >void f( const T& t, const U& u = null )
> > >{
> > >   std::cout << typeid( t ).name() << " - "
> > >             << typeid( u ).name() << std::endl;
> > >}
>
> Why does the standard allow me to specify a default parameter when it
> prevents the parameter to be used?

Does it?

Have you tried calling it like this:

    f<int,Null>(42);

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Daniel Frey <daniel.frey@aixigo.de>
Date: 19 Jun 2002 15:10:04 GMT
Raw View
Allan W wrote:
>
> Daniel Frey <daniel.frey@aixigo.de> wrote
> > > >class Null {} null;
> > > >
> > > >template< typename T, typename U >
> > > >void f( const T& t, const U& u = null )
> > > >{
> > > >   std::cout << typeid( t ).name() << " - "
> > > >             << typeid( u ).name() << std::endl;
> > > >}
> >
> > Why does the standard allow me to specify a default parameter when it
> > prevents the parameter to be used?
>
> Does it?

In certain situations. Please read the other, newer branch of this
thread first. I understand that the default will be used if you call f<
int, Null >( 42 ); but there are situations where the code just looks
strange. But the new standard (hopefully) will allow to specify a
default template parameter together with a default parameter, so the
code will become clearer.

Another problem was, that my compiler had a bug which made it accept the
following:

template< typename T > void f( T t, T u = 0 ) { ... }
template< typename T > void f( T t ) { f( t, 1 ); }

f( 42 ); // called f( 42, 1 );

instead of warning about ambiguous overload. But the GCC 3.1 warns about
this code.

> Have you tried calling it like this:
>
>     f<int,Null>(42);

This is not my point, I could also call it like this:

  f( 42, null );

But I won't need a default if I specify it explicitly.

Regards, Daniel

--
Daniel Frey

aixigo AG - financial training, research and technology
Schlo   -Rahe-Stra   e 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Graeme Prentice <gp1@paradise.net.nz>
Date: Fri, 7 Jun 2002 17:49:28 CST
Raw View
On Tue,  4 Jun 2002 17:14:56 GMT, Daniel Frey <daniel.frey@aixigo.de>
wrote:

>class Null {} null;
>
>template< typename T, typename U >
>void f( const T& t, const U& u = null )
>{
>   std::cout << typeid( t ).name() << " - "
>             << typeid( u ).name() << std::endl;
>}
>
>
>I observed the above pattern several times, but without the default
>parameter but with overloaded functions. And usually there are a lot of
>parameters, so the code looks ugly and bloated. Changing the language to
>allow the above could clean up the code...

you mean the compiler should deduce the type U from the default
parameter  - I guess that's possible  -  if you have a lot of default
parameters (as I've recently seen in some C++ code ) that could be
useful   - when there's only one or two, you can do this

template< typename T>
inline void f( const T& t)
{
   f( t , null );
}

I suspect that having more than 3 or 4 type parameters is pretty
uncommon though and probably makes hard to read code

Graeme

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Daniel Frey <daniel.frey@aixigo.de>
Date: Mon, 10 Jun 2002 09:00:53 GMT
Raw View
Graeme Prentice wrote:
>=20
> On Tue,  4 Jun 2002 17:14:56 GMT, Daniel Frey <daniel.frey@aixigo.de>
> wrote:
>=20
> >class Null {} null;
> >
> >template< typename T, typename U >
> >void f( const T& t, const U& u =3D null )
> >{
> >   std::cout << typeid( t ).name() << " - "
> >             << typeid( u ).name() << std::endl;
> >}
> >
> >
> >I observed the above pattern several times, but without the default
> >parameter but with overloaded functions. And usually there are a lot o=
f
> >parameters, so the code looks ugly and bloated. Changing the language =
to
> >allow the above could clean up the code...
>=20
> you mean the compiler should deduce the type U from the default
> parameter  - I guess that's possible  -  if you have a lot of default
> parameters (as I've recently seen in some C++ code ) that could be
> useful   - when there's only one or two, you can do this
>=20
> template< typename T>
> inline void f( const T& t)
> {
>    f( t , null );
> }

Well, this is - except for the 'inline' - my code from the original
post. Sure I know it can be done this way, but that's not my point.

> I suspect that having more than 3 or 4 type parameters is pretty
> uncommon though and probably makes hard to read code

Think of printf-replacements (formatters), functors, binders, etc. where
10 parameters and more would be useful. But currently, this ends up in
lots of helper functions like the one above.

Why does the standard allow me to specify a default parameter when it
prevents the parameter to be used? This doesn't make sense to me and it
seems dangerous. Consider a different default parameter plus the helper
function. Calling f( 42 ) result in a call to the helper function. But
reading the code, one might see the first function with a different
default parameter first. Don't you think that's dangerous?

There are IMHO two solutions. The first is a conservative approach:
Disallow default parameters for template functions. It may break code
that (superfluously) contains default parameters. The second solution
breaks existing code, too: Allow the compiler to actually use the
default parameter to deduce the type. All in all, a default parameter is
a parameter, isn't it? But I wouldn't expect a change to break any
existing code as no one can use default parameters for template
functions today.

Regards, Daniel

--
Daniel Frey

aixigo AG - financial training, research and technology
Schlo=DF-Rahe-Stra=DFe 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Daniel Frey <daniel.frey@aixigo.de>
Date: Tue, 4 Jun 2002 17:14:56 GMT
Raw View
Hi,

here's a little code-fragment that I would like to discuss:

#include <iostream>
#include <typeinfo>

class Null {} null;

template< typename T, typename U >
void f( const T& t, const U& u =3D null )
{
   std::cout << typeid( t ).name() << " - "
             << typeid( u ).name() << std::endl;
}

// Comment/uncomment this to make the example compile.
// If commented out, I receive a compiler error, see below...
// #define WORK

#ifdef WORK
template< typename T >
void f( const T& t )
{
   f( t, null );
}
#endif

int main()
{
   f( 42, 21 );
   f( 42 );
}

Now, the above code doesn't compile, both gcc and comeau reject the f(
42 )-call. They both say, that there is no matching function for call to
f(int). Are they right? I think so, but than, why is the default
parameter allowed for f's second argument? Would it be any problem to
change C++0x to make the example compile? What do you think?

I observed the above pattern several times, but without the default
parameter but with overloaded functions. And usually there are a lot of
parameters, so the code looks ugly and bloated. Changing the language to
allow the above could clean up the code...

Regards, Daniel

--
Daniel Frey

aixigo AG - financial training, research and technology
Schlo=DF-Rahe-Stra=DFe 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]