2011-10-22 3 views
1

는 내가 처리 파이프 라인 구현이,하지만 난 이런 식으로 개선하고 싶습니다 :가변 인수 템플릿 인수가있는이 생성자가 일치하지 않는 이유는 무엇입니까?

sda_variadic.cpp: In function 'int main()': 
sda_variadic.cpp:40:39: error: no matching function for call to 'pipeline<f1, f2>::pipeline(src&, b1&, snk&)' 
sda_variadic.cpp:40:39: note: candidates are: 
sda_variadic.cpp:26:5: note: template<class ... Buffs, class Bin, class Bout> pipeline<T1, T ...>::pipeline(Buffs& ..., Bin&, Bout&) 
sda_variadic.cpp:23:8: note: constexpr pipeline<f1, f2>::pipeline(const pipeline<f1, f2>&) 
sda_variadic.cpp:23:8: note: candidate expects 1 argument, 3 provided 
sda_variadic.cpp:23:8: note: constexpr pipeline<f1, f2>::pipeline(pipeline<f1, f2>&&) 
sda_variadic.cpp:23:8: note: candidate expects 1 argument, 3 provided 

이 오류의 원인은 무엇인가 :

#include <iostream> 

// buffers 
struct src{}; struct b1{}; struct snk{}; 
// filters 
struct f1 
{ 
    f1(const src &, b1 &) { std::cout << "f1(src, b1)" << std::endl; } 
}; 
struct f2 
{ 
    f2(const b1 &, snk &) { std::cout << "f2(b1, snk)" << std::endl; } 
}; 
// the pipeline 
template< typename... Filters > 
struct pipeline 
{ 
    template< typename LastB > 
    pipeline(const LastB &) 
    {} 
}; 
template < typename T1, typename... T > 
struct pipeline< T1, T... > : pipeline<T...> 
{ 
    template< typename... Buffs, typename Bin, typename Bout > 
    pipeline(Buffs &... buffs, Bin & bin, Bout & bout) : 
     pipeline<T...>(buffs..., bin), 
     filter(bin, bout) 
    { 
    } 

    T1 filter; 
}; 

int main() 
{ 
    src ba; b1 bb; snk bc; 

    pipeline<f1> p1(ba, bb); 
    pipeline< f1, f2 > p2(ba, bb, bc); // the problem is in this line! 
} 

불행하게도, 위의 예는 다음 오류를 생산?
해결 방법?

작은 설명. 위의 예에서는 먼저 특수화되지 않은 pipeline<>(snk)의 개체를 만든 다음 특수화 된 개체 pipeline<f1>(b1,snk)을 만든 다음 특수화 된 개체 pipeline< f1, f2 >(src,b1,snk)을 만듭니다.
btw 위 예제는 1 개의 필터 (pipeline< f1)에 적용됩니다. 함수 파라미터 팩 (Buffs)가 최종 위치에 아니므

답변

4
template< typename... Buffs, typename Bin, typename Bout > 
pipeline(Buffs &... buffs, Bin & bin, Bout & bout) : 
    pipeline<T...>(buffs..., bin), 
    filter(bin, bout) 
{ 
} 

, 그것은 추론 될 수 없다. 14.8.2.1에서 함수 호출 [temp.deduct.call] 제 1 항에서 템플릿 인수를 추론 :

  1. [...]를 파라미터 유효의 끝에서 발생하지 않는 함수 매개 변수 팩 선언 - 목록에서 매개 변수 팩의 유형은 추론되지 않은 컨텍스트입니다. [...]

명시 적으로 생성자에 템플릿 매개 변수를 전달할 수 없기 때문에, (비록이 문제에 대해 중요하지 않는 것이) 전혀 호출 할 수 없습니다.

pipeline(std::forward_as_tuple(b0, b1, b2), in, out)과 같이 생성자를 호출하여 가변 인수를 조작하고 빈 튜플이 마지막 스테이지로 전달되도록 std::tuple을 사용하는 것이 좋습니다.

관련 문제