람다 함수의 두 가지 다른 유형을 미리 템플릿 인수를 모른 채로 클래스 멤버로 받아 들일 수 있습니까? 이 같은 것을 할 수 있다는 것을람다가 클래스 멤버로 기능 함
struct two_functors {
std::function<???> a;
std::function<???> b;
...
};
같은 :
본질적으로 tuple의void main(){
vector<two_functors> many_functors;
int a = 2;
int b = 3;
double c = 4.7;
double d = 8.4;
two_functors add_and_subtract;
add_and_subtract.a = [a, b](int x, int y){cout << x + y << endl;};
add_and_subtract.b = [c, d](double x, double y){cout << x - y << endl;};
two_functors multiply_and_divide;
multiply_and_divide.a = [c, d](double x, double y){cout << x * y << endl;};
multiply_and_divide.b = [a, b](int x, int y){cout << x/y << endl;};
many_functors.push_back(add_and_subtract);
many_functors.push_back(multiply_and_divide);
for (auto functors : many_functors){
functors.a();
functors.b();
}
}
는 –
는'클래스 템플릿 수 two_functors' 수, 내가 기대 ... 템플릿을 사용하여 수행 할 수 있을까요? 'add_and_print.a()'와'add_and_print.b()'가'x','y','s'로 전달할 인자가없이 올바르지 않습니까? – aschepler
뭔가를 미리 선언해야하는 유일한 이유는 다른 것이 뭔가를 사용하는 경우입니다. 그래서 당신은 어떤 문구에'add_and_print'를 사용하고 어떤 내용을 담고 있지 않은지 알기를 기대합니다. 당신이하려는 일의 모범이 있습니까? – aschepler