auto ptr= new std::string("Hello World") ;
You have a pretty good idea what you're going to get back from new, and the compiler can figure it out, so here you don't have to say it twice.
But when you go to call some advanced routine in the standard libraries, and you really don't have any idea what you're going to get back but you want to save the value in a structure, then having the examples use auto is really annoying. This page on std::async left me very little idea what to do if I wanted to create a data structure that captured running threads:
auto handle = std::async(std::launch::async, parallel_sumOne has to guess at this point that the return values from async are very close to un-nameable, and use templates instead to capture them. This is the very thing that std::function does to wrap up lambdas which are another construct that is practically un-nameable. Interestingly enough, one place auto saves the day when not even template argument deduction would work is with another C++11 feature called initializer lists:, mid, end); int sum = parallel_sum(beg, mid); return sum + handle.get();
templated_fn({1, 2, 3}); // FAIL - initializer list has no type, and so T cannot be deduced auto al = {10, 11, 12}; // special magic for autothough you can get around the template failure by spelling things out:
templated_fn<std::initializer_list<int>>({1, 2, 3}); // OK
Labels: c++, c++11, programming
Feb '04
Oops I dropped by satellite.
New Jets create excitement in the air.
The audience is not listening.
Mar '04
Neat chemicals you don't want to mess with.
The Lack of Practise Effect
Apr '04
Scramjets take to the air
Doing dangerous things in the fire.
The Real Way to get a job
May '04
Checking out cool tools (with the kids)
A master geek (Ink Tank flashback)
How to play with your kids