이 함수에 대한 함수 포인터와 인수를 저장할 수있는 템플릿 클래스를 만들어 나중에이 인수를 사용하여 함수를 호출 할 수 있습니다.가변 인수를 사용하여 가변 인수를 저장하는 방법은 무엇입니까?
전적으로 이것을 쓰고 자하며 인수 유형이나 숫자에 의존하지 않으려합니다. 나는 그것이 가능하다면 적어도 안전이 클래스 유형의 공용 인터페이스를하고 싶은
template<class T, typename... Params>
class LazyEvaluation {
private:
// Function to be invoked later
T (*f)(Params...);
// Params for function f
Params... storedParams; // This line is not compilable!
bool evaluated;
T result;
public:
// Constructor remembers function pointer and parameters
LazyEvaluation(T (*f)(Params...),Params... params)
: f(f),
storedParams(params) //this line also cannot be compiled
{}
// Method which can be called later to evaluate stored function with stored arguments
operator T&() {
// if not evaluated then evaluate
if (! evaluated) {
result = f(storedParams...);
evaluated = true;
}
return result;
}
}
: 여기
는 C++ 11의 가변 인자 템플릿의 사용과 아이디어의 scatch입니다. 적어도이 일을하는 것이 더 중요합니다.나는 가변 인수를 어떻게 든 저장했다. 그러나 나는 그것들을 함수 f에 전달할 수 없었다. 나는 그것을 답에 쓰겠지만, 제 추악한 일하지 않는 시도를보기 전에 당신이 당신의 해결책에 대해 생각해 주길 바랍니다.
위의 코드를 Microsoft Visual C++ 컴파일러 11 월 2012 CTP (v120_CTP_Nov2012)로 컴파일하려고하지만 컴파일러 독립적 인 솔루션이있는 것이 가장 좋습니다.
parametr 팩을 확장 recursivle 될 수 있으며, 각 parametr 저장 :
는 여기
난이 중복이라고 생각 : http://stackoverflow.com/questions/14833129/a-clean-way-to-store-a-function-and-its-arbitrary-type-arbitrary-number-argum – hmjd
그것들을'std :: tuple'에 저장하십시오 ... –
kennytm
@KennyTM 그것은 답이되어야합니다. – Angew