나는 추적의 종류의 펑 있습니다템플릿 인수를 줄이는 방법은 무엇입니까? 여기
template<class T, class Foo, T Foo::*p>
struct X {
void operator()(Foo & f) {
(f.*p) = 12 * (f.*p); // simple example. could be more complex `operator()`
}
};
그리고 샘플 구조체 :
struct FF
{
int m;
int r;
};
나는 펑터 X를 사용하고 싶지만, 나는 다음과 같이 명시 적으로 템플릿 인수를 지정하지 않으
: 내가 성공하지 시도 여기에서void testforx()
{
std::vector<FF> cont(5);
std::for_each(cont.begin(), cont.end(), X<int, FF, &FF::r>()); // it work, but I don't want to write `int` and `FF`
std::for_each(cont.begin(), cont.end(), createx<&FF::r>()); // how I would like to use it, how to declare `createx` function?
}
이
template<class T, class Foo>
struct X {
X(T Foo::*p): p(p) {}
void operator()(Foo & f) {
(f.*p) = 12 * (f.*p); // simple example. could be more complex `operator()`
}
private:
T Foo::*p;
};
template <class T, class Foo>
X<T, Foo> MakeX(T Foo::*p)
{
return p;
}
나는 당신의 접근 방식과 유형을 추론 할 수있다 생각하지 않는다 : 당신은 포인터에 사용할 수 없습니다
작은 수정 ("올바른"코드가 컴파일되지 않음) :'createx'의 리턴 타입 선언은'T Foo :: * p'가 아닌 세 번째 템플릿 매개 변수로'p'를 포함해야합니다. 그러나 문제는 변하지 않습니다. –