Ho un modello di funzione di lavoro che chiama un lambda.Come utilizzare l'inoltro perfetto variadic in una lambda?
Vorrei generalizzare questo modello di funzione per prendere argomenti variadici e inoltrarli perfettamente nel lambda, ma ho difficoltà a compilare questo codice.
Sto utilizzando gcc 4.7.2.
UPDATE
Utilizzando il suggerimento di R. Martinho Fernandes, ho guardato l'errore in Bugzilla - it does look like a bug that's been around for a while. Se qualcuno sa di una soluzione alternativa (ne sto cercando uno ora), si prega di inviare una risposta.
ERRORI
junk.cpp: In lambda function:
junk.cpp:32:68: error: parameter packs not expanded with ‘...’:
junk.cpp:32:68: note: ‘args’
junk.cpp: In instantiation of ‘std::pair<std::basic_string<char>, typename T::Lambda> MP(const string&, M, Args&& ...) [with T = Integer; M = int (Integer::*)()const; Args = {}; typename T::Lambda = std::function<std::function<int()>(const Integer&)>; std::string = std::basic_string<char>]’:
junk.cpp:47:42: required from here
junk.cpp:34:2: error: using invalid field ‘MP(const string&, M, Args&& ...)::<lambda(const T&)>::__args’
make: *** [junk] Error 1
CODICE
#include <functional>
#include <iostream>
#include <map>
struct Integer
{
typedef std::function<int()> Function;
typedef std::function<Function(Integer const& inst)> Lambda;
virtual int getInt() const = 0;
};
struct IntImpl : public Integer
{
virtual int getInt() const { return 42; }
};
typedef std::function<int()> IntFunction;
typedef std::function<IntFunction(Integer const& inst)> IntLambda;
#define WONT_COMPILE
template<typename T,typename M,typename... Args>
std::pair<std::string,typename T::Lambda>
MP(std::string const& str, M method, Args&&... args)
{
#ifdef WONT_COMPILE
return std::make_pair(str,
[=](T const& inst)
{
// COMPILE ERROR (Line 32) on next line
return std::bind(method, std::cref(inst), std::forward<Args>(args)...);
}
);
#else
return std::make_pair(str,
[method](T const& inst)
{
return std::bind(method, std::cref(inst));
}
);
#endif
}
std::map<std::string,IntLambda> const g_intTbl =
{
MP<Integer>("getInt", &Integer::getInt)
};
int
main(int argv, char* argc[])
{
IntImpl x;
std::cerr << g_intTbl.find("getInt")->second(x)() << std::endl;
}
Alcuni di questi errori sembrano verificarsi perché 'std :: avanti (args ...)' dovrebbe essere 'std :: avanti (args) ...' –
@AndreiTita +1 ty - che aiuta - I sto ancora ricevendo degli errori - OP è stato aggiornato – kfmfe04
Beh, non so se ti è permesso espandere i pacchetti variadic nelle catture lambda (lo verificherò tra un momento), ma una soluzione alternativa sarebbe usare una cattura completa come '[=]'. –