I am experimenting using lambda and say hello when testing the next compilation.
auto lmda = [](std::ostream& os) -> std::ostream& { os << "hi"; return os; }; std::cout << lmda;
But when you add a capture, it does not compile. Example:
std::vector<int> v(5, 3); auto lmda = [&v](std::ostream& os) -> std::ostream& { os << v.size(); return os; }; std::cout << lmda;
Build Error:
In function 'int main()': 10:18: error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&' In file included from /usr/include/c++/4.9/iostream:39:0, from 2: /usr/include/c++/4.9/ostream:602:5: note: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = main()::<lambda(std::ostream&)>]' operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
I do not understand why in the second example this fails. Any guidance?
c ++ lambda c ++ 11
Mohamed b
source share