Re: Question regarding use of C++0x auto keyword
* markfrey@fastmail.fm:
On Feb 6, 1:25 am, "Bo Persson" <b...@gmb.dk> wrote:
markf...@fastmail.fm wrote:
still limited to the life of "f(mc.getSomething());". When using
the auto keyword however, we've extended the life of the converter
object (or copies of it) until we reach the end of the enclosing
block. If the converter object held references to the myclass
object, and the myclass object goes away before the end of the
enclosing block, bad things happen.
Isn't it your responsiblility as a class designer to assure that this
doesn't happen? Why would you have a public function returning objects
containing references to other objects that goes away?
Well yes, but I can't very well assure this doesn't happen in the face
of future language features that don't exist when I write the code. As
far as I can tell, without the auto keyword, there is no way the
returned object can live beyond the lifetime of the returning object.
That's my main concern with auto. Were it implemented to effectively
just replace the word auto with the correctly deduced type and then
compile as before it wouldn't change the behavior in question. I guess
I'm having trouble seeing the benefit of it working this way when it
changes the semantics of the language and doesn't seem to add any new
functionality.
I agree with your sentiment and position, but the argument is incorrect.
Rather than changing the language semantics, what 'auto' does is to make it much
/easier/ to construct a copy of an instance of some inaccessible class.
I.e., 'auto', with its current definition, makes it easy, far too easy, to do
that /inadvertently/.
But it is possible within the current language.
For example -- showing the hoops you have to jump through to do it without
'auto', whereas with 'auto' you can very easily do such things inadvertently:
<code>
#include <memory>
#include <iostream>
class MyClass
{
private:
struct converter
{
converter( int n ) : n_( n ) { /*empty*/ }
operator int() const { return(n_); }
int n_;
};
public:
converter getSomething() { return converter( 666 ); }
};
class ConverterWrapper
{
private:
struct IntProducer
{
virtual ~IntProducer() {}
virtual int value() const = 0;
};
template< typename T >
struct IntProducerWrapper: IntProducer
{
T myProducer;
IntProducerWrapper( T const& x )
: myProducer( x )
{}
int value() const { return myProducer.operator int(); }
};
ConverterWrapper( ConverterWrapper const& );
ConverterWrapper& operator=( ConverterWrapper const& );
std::auto_ptr<IntProducer> myProducerPtr;
public:
template< typename T >
ConverterWrapper( T const& x )
: myProducerPtr( new IntProducerWrapper<T>( x ) )
{}
int value() const { return myProducerPtr->value(); }
};
int main()
{
MyClass mc;
ConverterWrapper wrapper( mc.getSomething() );
std::cout << wrapper.value() << std::endl;
}
</code>
Cheers & hth.,
- Alf
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]