Sarebbe bello se potessi fare quanto segue:supportati da cattura lambda argomenti template variadic
template <class RT, class... PT>
class Event
{
...
void operator()(PT... args)
{
std::for_each(
l.begin(), l.end(), [args...](Handler *p) { (*p)(args...); }
);
}
...
};
purtroppo non ho potuto farlo compilare con g ++ 4.7.2 (-std = C++ 0x) :
evtempl.hh: In member function 'void elt::Event::operator()(PT ...)': evtempl.hh:75:54: error: expected ',' before '...' token evtempl.hh:75:54: error: expected identifier before '...' token evtempl.hh:75:57: error: parameter packs not expanded with '...': evtempl.hh:75:57: note: 'args' evtempl.hh: In lambda function: evtempl.hh:76:26: error: expansion pattern 'args' contains no argument packs evtempl.hh: In instantiation of 'void elt::Event::operator()(PT ...) [with RT = void; PT = {int}]': testevtempl.cc:28:9: required from here evtempl.hh:74:9: error: using invalid field 'elt::Event::operator()(PT ...)::::Handler*)>::__args' evtempl.hh: In instantiation of 'void elt::Event::operator()(PT ...) [with RT = void; PT = {int, const char*}]': testevtempl.cc:29:20: required from here evtempl.hh:74:9: error: using invalid field 'elt::Event::operator()(PT ...)::::Handler*)>::__args'
invece, devo cambiare che lambda al vecchio, la sintassi banale:
for (itr = l.begin(); itr != l.end(); ++itr)
(*(*itr))(args...);
Questo compila e funziona fin e.
Mi chiedo perché la sintassi lambda non funzioni.
- Ho fatto qualcosa di sbagliato o mi sono perso qualcosa?
- Tale scopo è dichiarato illegale nello standard C++ 11?
- o, questo è consentito dallo standard, ma è un problema del compilatore corrente?
ho cercato
[=](Handler *p) { (*p)(args...); }
dà lo stesso errore, come se l'avete fatto:
[args](Handler *p) { (*p)(args...); }
parametro lamentarsi non pacchi ampliato
Hai provato '[=]' invece? –
@Kerrek SB buon suggerimento, ma non funziona –
Se si isola il lambda 'auto foo = [=] (Handler * p) {(* p) (args ...); }; ', ottieni due errori" 1) Il pacchetto di parametri non è espanso e 2) il modello di espansione "args" non contiene pacchetti di parametri ". Sospetto che si tratti di un bug di GCC, poiché si lamenta che Arg non è un pacchetto e deve essere uno. –