Topic: Core Language issue 226 (was: Default argument specified but not


Author: Daniel Frey <daniel.frey@aixigo.de>
Date: Fri, 21 Jun 2002 15:33:47 GMT
Raw View
Francis Glassborow wrote:
>=20
> In article <3D0DAA10.F2E3D027@aixigo.de>, Daniel Frey
> <daniel.frey@aixigo.de> writes
> >But I can live with having to provide 'typename U =3D Null' additional=
ly
> >to 'const U& u =3D null'. Still C++ proves its complexity once again
> >(IMHO). I hope that compilers will at least give a warning about a
> >possible ambiguous situation for code like the example I gave as it is
> >not obvious why the default is ignored and another function is called
> >instead.
>=20
> As we are considering function templates why not just overload the
> template function. This is just one more place where pre-occupation wit=
h
> default parameters buys nothing.

As you can always use overloads instead of default parameters, why do we
need default parameters anyway? Because it keeps your code clearer and
easier to maintain. This tread originated in a function to format
strings. It is similar to printf, but it should be type safe. The
function basically looks like this:

template< typename T0,
          typename T1,
          typename T2,
          ...,
          typename Tn >
std::string format( const std::string& formatter,
                    const T0& arg0,
                    const T1& arg1,
                    const T2& arg2,
                    ...,
                    const Tn& argn )
{
   ...
}

As I cannot use default parameters to allow

   format( "Dec: [0], Hex: [0:%08x]", 1234 );

I have to add n-1 helper functions. This works, but it's not what I like
to write or maintain. When using function objects (functors), this is
also a common pattern.

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: Mon, 17 Jun 2002 18:01:17 GMT
Raw View
Francis Glassborow wrote:
>=20
> In article <3D0A18F5.2006BA9C@aixigo.de>, Daniel Frey
> <daniel.frey@aixigo.de> writes
> >> template< typename T, typename U >
>=20
> At this point you make a promise that either the compiler will be able
> to deduce the types of T and U or you will provide them explicitly.
>=20
> >> void f( const T& t, const U& u =3D null )
> At this point the compiler needs a value of null for whatever type U is.
> Note that despite your definition above, U might be a type with a null
> value provided in its namespace. The compiler cannot, at this stage,
> deduce that U must be Null, it must first know what type U represents
> and then seek a value 'null' for it.

Thanks for the explanation, but wouldn't it be reasonable to deduce a
default template parameter from the default parameter at time of the
instantiation? Some kind of "implicit default template parameter"? Just
consider that default is a string that is placed in the line that calls
the function with too few arguments. Just the same as writing f( 42,
null ) and see if the compiler finds a 'null' and a type for it.

But I can live with having to provide 'typename U =3D Null' additionally
to 'const U& u =3D null'. Still C++ proves its complexity once again
(IMHO). I hope that compilers will at least give a warning about a
possible ambiguous situation for code like the example I gave as it is
not obvious why the default is ignored and another function is called
instead.

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                       ]