I have the following code that compiles and works correctly due to such a concept as Input/output manipulators:
#include <iostream>
#include <ostream>
struct st{
auto mke(){
return [](std::ostream& os) -> decltype(auto) {return os<<42;};
}
};
int main(){
std::cout<<st{}.mke();
}
But actually, in my project, I need to capture this
by reference and output some fields of the structure. Something like this:
#include <iostream>
#include <ostream>
struct st{
int a=42;
auto mke(){
return [this](std::ostream& os) -> decltype(auto) {return os<<a;};
}
};
int main(){
std::cout<<st{}.mke();
}
But it doesn’t compile.
Q: I need a detailed explanation of the reasons preventing the compilation of the second example. What really happened?
0