저는 인덱스가있는 SQL 테이블과 유사한 몇 가지 std :: vectors에 가변 arity 함수를 가져와야하는 기중 장치를 작성하고 있습니다. 모든 벡터에서 같은 id를 갖는 각 요소 집합에 f 매개 변수를 적용하고 싶습니다. 템플릿 공제에 문제가 있습니다. 여기에서 이터레이터 로직을 뺀 곳에서 추출한 것입니다. 필자는 합리적인 재귀 케이스라고 생각하지만 필기 템플릿 공제는 실현 가능하지 않다고 생각합니다.하나의 가변 템플릿 매개 변수를 새로 만들 때 사용할 수 있습니까?
// Simple container type
template <typename T>
struct A {
T x;
T y;
T z;
};
// Wrapper type to differentiate an extracted value from a container
// in template deduction
template <typename T>
struct B {
T value;
};
// Base case. All A<Ts> have been extracted to B<Ts>.
template <typename F, typename... Ts>
void lift (F f, B<Ts> ...bs) {
// Call f with extracted values
f(bs.value...);
}
// Recursive case
template <typename F, typename T, typename... Ts, typename Us>
void lift (F f, A<T> a, A<Ts> ...as, B<Us> ...bs) {
// Copy a value from the beheaded container A<T>.
B<T> b = {a.x};
// Append this B<T> to args and continue beheading A<Ts>.
lift(f, as..., bs..., b);
}
// Test function
template <typename... Ts>
struct Test {
void operator() (Ts...) {}
};
int main() {
B<int> b = {1};
// No errors for the base case
lift(Test<>());
lift(Test<int, int>(), b, b);
// error: no matching function for call to 'lift'
// The notes refer to the recursive case
A<int> a = {1,0,0};
lift(Test<int>(), a); // note: requires at least 3 arguments, but 2 were provided
lift(Test<int>(), a, a); // note: requires 2 arguments, but 3 were provided
lift(Test<int>(), a, a, b); // note: requires 2 arguments, but 4 were provided
}
이 코드의 잘못된 점은 무엇입니까?
모든 것을 무시하고 읽고 쓰기가 쉽습니다. 왜이 컴파일이 실 거예요?
'as' 팩은 마지막 매개 변수 위치에 있지 않기 때문에 추론 할 수 없습니다. –