W
werasm
Hi all,
I just want to clarify this example:
#include <memory>
struct X
{
virtual std::auto_ptr<X> clone() const;
//etc...
};
struct Y
{
virtual std::auto_ptr<Y> clone() const;
//etc...
};
template <class T>
class Opt
{
public:
Opt( const T& ref ): p_( &ref ){ }
std::auto_ptr<T> clone( T* ) const
{ return p_->clone(); }
private:
const T* p_;
};
struct OptXY : Opt<X>, Opt<Y>
{
OptXY( const X& x, const Y& y )
: Opt<X>( x ), Opt<Y>( y ){ }
};
void foo( const Opt<X>&, const Opt<Y>&, const OptXY& );
int main()
{
foo(
Opt<X>( X() ),
Opt<Y>( Y() ),
OptXY( X(), Y() ) );
}
If I understand 12.2.5 correctly, then all the temporaries will be
destroyed at the end of the body of foo, the order being opposite
to the order of creation (which is undetermined). It would then
be safe to say that they are usable (and the clone is safe)
anywhere within the body of foo. Is this right?
Regards,
Werner
I just want to clarify this example:
#include <memory>
struct X
{
virtual std::auto_ptr<X> clone() const;
//etc...
};
struct Y
{
virtual std::auto_ptr<Y> clone() const;
//etc...
};
template <class T>
class Opt
{
public:
Opt( const T& ref ): p_( &ref ){ }
std::auto_ptr<T> clone( T* ) const
{ return p_->clone(); }
private:
const T* p_;
};
struct OptXY : Opt<X>, Opt<Y>
{
OptXY( const X& x, const Y& y )
: Opt<X>( x ), Opt<Y>( y ){ }
};
void foo( const Opt<X>&, const Opt<Y>&, const OptXY& );
int main()
{
foo(
Opt<X>( X() ),
Opt<Y>( Y() ),
OptXY( X(), Y() ) );
}
If I understand 12.2.5 correctly, then all the temporaries will be
destroyed at the end of the body of foo, the order being opposite
to the order of creation (which is undetermined). It would then
be safe to say that they are usable (and the clone is safe)
anywhere within the body of foo. Is this right?
Regards,
Werner