Rcpp11 / rjournal_article

C++11 Article for the R journal
1 stars 0 forks source link

Ideas: auto #8

Open kevinushey opened 10 years ago

kevinushey commented 10 years ago

Probably best described in the context of types, in the sense that they bring the static typing of C++ a bit closer to the dynamic typing of R. The crux being, auto means we don't need to worry about types, somewhat similar to R. There are caveats, of course.

Also, it allows us to avoid some typedefs, e.g. auto itr = x.begin() is pretty well understood from context to be an iterator over whatever collection x encompasses.

Might even be worth mentioning generic lambdas coming with C++14, where even the function arguments can be auto, especially since I believe the development versions of clang / gcc support them. So it'll be easily available for linux, and I'm guessing the next version of apple clang may even have support.

romainfrancois commented 10 years ago

A few things I like about auto: it is nice combined for range based for loops

std::unordered_map<int,T> map ; 
for( auto x : map){
    // do stuff
}

could be interesting to see what happens with sugar where the types made by a sugar expression are potentially complex to type. e.g.

NumericVector x, y ;
auto left = x < 2* y ;
auto right = x*x > 3 ;
auto res = ifelse( x < y, left, right ) ;  

Without C++11, if we want to split the expressions into pieces we have to resolve them in a vector, which need to allocate memory, etc ...

If we talk about auto, it is probably worth talking about decltype too.

ggrothendieck commented 9 years ago

fyi, there is a relevant example of auto here:

https://github.com/RcppCore/Rcpp/issues/218