나는 람다를 호출하는 작동 함수 템플릿을 가지고있다.람다로 variadic 완벽한 전달을 어떻게 사용합니까?
이 함수 템플릿을 가변 인수를 취하여 람다로 완벽하게 전달하려면이 함수 템플릿을 일반화하고 싶지만이 코드를 컴파일하는 데 문제가 있습니다.
gcc 4.7.2를 사용하고 있습니다.
UPDATE
R. 마르틴 페르난데스의 제안을 사용하여
, 나는 버그질라에 오류를 보았다 - it does look like a bug that's been around for a while. 만약 누군가가 해결 방법을 알고 있다면 (나는 지금 한 가지를 파헤 치고있다.) 대답을 게시 해주십시오.에러
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
CODE이 (아직 수행하지 않은 경우를보고하십시오) 컴파일러의 오류로 나타납니다
#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;
}
'std :: forward (args ...)'이 (가)'std :: forward가되어야하기 때문에 발생하는 것으로 보인다. :: (args) ... ' –
@AndreiTita +1 ty - 도움이된다. 여전히 오류가 발생합니다 - OP가 업데이트되었습니다. – kfmfe04
람다 캡처에서 가변 팩을 확장 할 수 있는지 잘 모르겠지만 (잠시 후에 확인할 것입니다.) 해결 방법은 다음과 같이 전체 캡처를 사용하는 것과 같습니다. '[=]'. –