Boost logo

Boost :

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


> Your are asking why can't the constructor be not explicit, right?
>
> Well, this would allow the 'direct' syntax fn(1,3) that
> Vincent wanted,
> but...
>
> It would entirely break the pointer semantics because the
> following would be
> allowed:
>
> void foo()
> {
> optional<int> opt ;
> opt = 3 ; // This will construct a temporary optional<int>(3)
> if ( opt == 0 ) // tmp optional<int>(0) here.
> }

You may poison undesired operators.
 
> Even if it would be allowed to have optional<int&>, you would
> still have to
> use it as if it were a pointer:
>
> void foo ( optional<int&> p = optional<int&>() )
> {
> if ( !!p ) // or ( peek(p) ) or ( initialized(p) )

BTW why do you need 3 methods that doing the same?

> {
> int& the_p = *p ;
> the_p = 3 ;
> etc...
> }
> }

Here 2 questions:

1. Let say we have

struct A { void moo() { ... } };

a. void foo( A* a ) { a->moo(); }
b. void foo( A& a ) { a.moo(); } // here we could assume inlining

Does both above versions are equaivalent form assembler stand point?

2. Now let say we use optional:

void foo( optional<A&> a ) { (*a).moo(); }

>From assembler stand point will it be equivalent to 1.a or 1.b?

And here yet another example where optional<T&> would be useful:

Let say I am using ostream wrapper for my printing and want to separate the
case whgen ostream& is not supplied:

class Log
{
....
    optional<ostream&> m_output;
{

And use it for example like this:

template<typename T>
Log::operator<<( T const& t )
{
    if( !!m_output )
     *m_output << t;
}

Would it be equivalent to reference or pointer? Or it the same in both
cases?

Gennadiy.


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