c++11 - How to simplify the tedious parameter declaration of lambdas in C++0x? -


the simplest code best asker:

#include <vector> #include <algorithm>  using namespace std;  int main() {     vector<int> coll;     for_each(coll.begin(), coll.end(), [](vector<int>::value_type n) -> void {});      return 0; } 

here, vector<int>::value_type n tedious. want have auto-like utility deduce right type of n automatically; following:

for_each(coll.begin(), coll.end(), [](auto_type n) -> void {}); 

to more greedy, want auto_type take argument used deduce right type of n. argument can (smart) pointer or reference container, or iterator of container.

dear gurus, how implement that?

you don't have declare void return in function. use decltype, like, decltype(coll[0]).

std::for_each(coll.begin(), coll.end(), [](decltype(coll[0]) value) {     std::cout << value; }); 

edit:

template<typename t> auto type(t&& t) -> decltype(*std::forward<t>(t).begin()) {     return *t.begin(); } std::for_each(coll.begin(), coll.end(), [](decltype(type(coll)) value) { }); 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -