다음과 같이 정의 된 벡터가 있고 std::vector<std::function<void()>> functions
항목을 저장하기 위해 다음과 같이 함수를 사용합니다.
template <typename Fun, typename Instance, typename ... Args>
void AddFunction(std::string name, Fun&& fun, Instance* instance, Args&& ... args)
{
/*something is going on here*/
functions.push_back(std::bind(fun, instance, std::forward<Args>(args)...));
/*something is going on here*/
}
문제는 때때로 전달 된 함수가 std::bind
어떤 종류의 값을 반환 할 수 있다는 것입니다. 나는 람다를 사용하는 것에 대해 생각했고 이런 식으로 시도했습니다.
template <typename Fun, typename Instance, typename ... Args>
void AddFunction(std::string name, Fun&& fun, Instance* instance, Args&& ... args)
{
/*something is going on here*/
auto lambda = [&]() -> void
{
fun(std::forward<Args>(args)...);
};
functions.push_back(std::move(lambda));
/*something is going on here*/
}
그러나 내가 사용하려고 할 때마다 내가 전달하는 함수가 취하는 인수의 수는 어디에 AddFunction
있다는 오류 메시지가 나타납니다 .term does not evaluate to a function taking n arguments
n
AddFunction
std::function<void()>
저장된 함수가 값을 반환하더라도 유형의 항목을 저장할 수 있도록 몇 가지 해결 방법이 있습니까?
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다