| ||||
| Comment on GotW #93 Solution: Auto Variables, Part 2 by Amali
// best auto get_size = [](const auto& x) { return x.size(); }; This code for auto lambda parameter types gives compiler error messages in gcc 4.8.0. Is there a compiler where it will work? Comment on GotW #93 Solution: Auto Variables, Part 2 by Ralph Tandetzky
Just for your information: These named lambdas with auto parameters have a very interesting use-case which makes them more flexible than the template function alternative. For instance, if you have a template function template <typename T> void func( T t ) { /*...*/ } and a generic template function template <typename F, typename T> void call( F f, T && t ) { f(forward<T>(t)); } then the following code will not work call( func, 42 ); since I would have to write func instead of func here. However this code should work, because func(42) is a valid expression. I actually had this problem in real world code in a physics simulation software I wrote. I can fix it by manually inserting the but with C++14 lambda with auto parameters it’s much easier and even less code. I just have to replace the definition of the func template function by static const auto func = []( auto T) { /*...*/ } This is even less typing. It makes the code more flexibly usable. I also wondered, if it might be generally a good idea to write named lambdas with auto parameters instead of template functions where possible. This might be food for another GotW. ;) By the way, I posted this stuff on Stackoverflow: http://stackoverflow.com/questions/17169498/why-do-i-need-to-specify-the-template-argument-type-of-a-templated-function-here It is possible to write this code in old C++ also, but it’s a bit more verbose. It’s gonna work with struct { template <typename T> void operator()(T t) { /* ... */ } } func; This is what I’m going to do for my code until C++14 is implemented. Read More »Comment on GotW #93 Solution: Auto Variables, Part 2 by Ralph Tandetzky
I just asked a question on StackOverflow about what the differences between template functions and name lambdas with auto parameters is. Some good answers are coming in. See http://stackoverflow.com/questions/17188485/template-functions-versus-named-lambdas-with-auto-parameters Read More »Comment on GotW #93 Solution: Auto Variables, Part 2 by Pablo Halpern
In 1(d), the original version creates a function which, when invoked, returns void. The alternative versions return size_type. I think you meant something like function<size_t(vector<int>)> Nice overview of the benefits of auto. It contained some things I had not thought of. Note that there is at least one situation in which auto is [b]extremely[/b] dangerous: expressions that return proxy types. Consider this problem: vector<bool> v = { ... }; auto b = v.back(); // Oops! b is of type vector<bool>::reference, which is NOT a bool v.pop_back(); bool a = b; // Oops! b has been invalidated! Some members of the standards committee have suggested ways to fix this in the language, but I am not familiar with specific proposals. Read More » | ||||
| ||||
Wednesday, June 19, 2013
FeedaMail: Comments for Sutterâs Mill
Subscribe to:
Comments (Atom)