Si consideri il seguente codice:L'acquisizione lambda causa errori di operando incompatibili?
main()
{
bool t;
...
std::function<bool (bool)> f = t ? [](bool b) { return b; } : [](bool b) { return !b; }; // OK
std::function<bool (bool)> f = t ? [t](bool b) { return t == b; } : [t](bool b) { return t != b; }; // error
}
quando si compila con Clang 3.1, l'assegnazione di non-cattura lambda funziona, mentre quello con cattura fallisce:
main.cpp:12:36: error: incompatible operand types ('<lambda at main.cpp:12:38>' and '<lambda at main.cpp:12:71>')
std::function<bool (bool)> f2 = t ? [t](bool b) { return t == b; } : [t](bool b) { return t != b; }; // error
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Perché catturare la stessa variabile fa sì che il 2 lambda essere di tipi incompatibili?
Grazie per la spiegazione dettagliata. Non sapevo che fossero implementati in modo diverso. Ha senso ora. –