Jakob Bieling wrote in news:d5*************@news.t-online.com in
comp.lang.c++:
Excuse the question .. but what exactly do I gain by using these
keywords? Apart from the fact that possibly I have to type less. Is
there anything I can do with these keywords, that I could not do
without them, or is this pure syntactic sugar like -> is?
thanks
Assume a set of overloaded functions:
some_type f( arg1_type a, arg2_type b );
Then:
template < typename A, typename B >
void contrived( A a, B b )
{
auto r = f( a, b );
std::cout << r << r;
}
Curently the /simple/ way to write the above means calling f(a, b)
twice, with possible side effects and expensive computations
accuring twice.
template < typename A, typename B >
void contrived( A a, B b )
{
std::cout << f( a, b ) << f( a, b );
}
Off the top of my head I've come up with two workaraounds:
1) Use indirection, i.e. put the body of contived in a
helper function, but that could be difficult with a less
simplistic example, or
2) Capture the return of f(a, b) in some kind of holder
object, but that would require an allocation and 2 virtual calls
(one for operator << and one for the destructor).
I'm sure there are others.
The basics of the proposal mean it will be simpler to write clear
and efficient code.
There is however a lot more to the auto/decltype proposal, but
it isn't that long so I suggest you give it a read.
Rob.
--
http://www.victim-prime.dsl.pipex.com/