|
Boost : |
Subject: Re: [boost] Library design Q : overloads v/s default args
From: Niels Dekker - address until 2010-10-10 (niels_address_until_2010-10-10_at_[hidden])
Date: 2010-06-20 05:39:15
Roshan wrote:
> The C++ Standard library does not use default arguments.
> Instead it provides an additional overload. Anybody
> know why ?
>
> For e.g.
>
> It provides the following two overloads for accumulate :
>
> template<class Itr, class T>
> accumulate(Itr first, Itr last, T init);
>
> template<class Itr, class T, class BinOp>
> accumulate(Itr first, Itr last, T init, BinOp op);
>
> instead of:
>
> template<class Itr, class T, class BinOp>
> accumulate(Itr first, Itr last, T init, BinOp op = std::plus<T>() );
N3092 (C++0x FDC), 17.6.4.5 Member functions [member.functions] says :
> An implementation may declare additional non-virtual member function
> signatures within a class:
> by adding arguments with default values to a member function
> signature;190 [ Note: An implementation may not add arguments with
> default values to virtual, global, or non-member functions. -end note ]
> by replacing a member function signature with default values by two
> or more member function signatures with equivalent behavior; and...
So an implementation of the Standard Library is allowed to use default
arguments, instead of having additional overloads, as long as they
behave the same.
> I am wondering if there is any reason to avoid default arguments in
> general purpose libraries ?
The additional overloads /might/ perform slightly better in some cases,
because they would avoid constructing default values, and passing them
as argument.
HTH, Niels
-- Niels Dekker http://www.xs4all.nl/~nd/dekkerware Scientific programmer at LKEB, Leiden University Medical Center
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk