Boost logo

Boost :

From: Rozental, Gennadiy (gennadiy.rozental_at_[hidden])
Date: 2002-11-22 14:14:46


> Good point. On a few ocasions I have use optional<> to pass optional
> parameters.
> However, I've came to the following:
>
> Take you example for instance:
>
> void fn(int iImportant, optional<int> iNotImportant = optional<int>())
> {
> if ( !!iNotImportant )
> {
> // not important argument recieved, use it.
> foo ( * iNotImportant ) ;
> }
> }
>
> Since optional<> uses pointer semantics, the above code could
> have been
> written using a true pointer with nearly the same syntatic
> usage of the
> optional parameter:
>
> void fn(int iImportant, int* iNotImportant = NULL )
> {
> if ( !!iNotImportant )
> {
> // not important argument recieved, use it.
> foo ( * iNotImportant ) ;
> }
> }

We already talked about this: pointer will add extra memory access, optional
should not (in fact it should be inlined and won't be different from by
value parameter)

> The constructor must be explicit in order to disable
> unexpected implicit
> conversions.

Some may still prefer sometime

foo(3)

to

foo(&lvalue(3));
foo(optional<T>(3));
 
implicit conversion to optional should not be dangerous anyway.

> Actually, optional<T> does not require T to be default constructable.
> The reason why you can't have optional<T&> is -at least- that
> you cannot
> have a reference to a reference,
> and optional<T> uses "T {const} &" (for example, in the constructor).

use add_reference instead?

Gennadiy.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk