2016-08-15 3 views
1

매개 변수 팩이 가변 매개 변수 함수에서 마지막 매개 변수가 아닌 경우 어떻게 작동하는지 이해하려고합니다. 내 샘플 코드에서 일부 호출이 작동하지 않는 이유는 모르겠다. 질문은 코멘트에 있습니다. 그들은 작동해야합니까 또는 뭔가를 이해하지 못하거나 VS2015 업데이트 3의 컴파일러가 아직 그들을 지원하지 않습니다? 함수 템플릿, 매개 변수 팩의 종류가에서의 경우에만 추론 할 수 있기 때문에마지막 매개 변수 팩 컴파일 오류

template <typename T> 
double sum(T t) { 
    return t; 
} 

template <typename T, typename... Rest> 
double sum(Rest... rest, T t) { 
    return t + sum(rest...); 
} 

template <typename T> 
double sum2(T t) { 
    return t; 
} 

template <typename T, typename... Rest> 
double sum2(T t, Rest... rest) { 
    return t + sum2(rest...); 
} 

template<typename... Args> 
void func(Args..., int = 0) 
{} 

void func() 
{ 
    func<int, int, int>(1, 1, 1); 
    func(1, 1, 1); // why doesn't this compile? 
    sum(1, 1); // why doesn't this compile? 
    sum<int, int>(1, 1); 
    sum<int, int, int>(1, 1, 1); // why doesn't this compile while func<int, int, int>(1, 1, 1) does? 

    // why are these compile? I only changed the order of the parameters compared to sum() 
    sum2(1); 
    sum2(1, 1); 
    sum2(1, 1, 1); 
} 

답변

2

나는 내가 아는 한, 정말 전문가가 아니에요하지만 ...

func(1, 1, 1); // why doesn't this compile? 

마지막 위치.

매개 변수의 유형이 도출되지 않고 설명 (Args...int, int, int이다)와 func()이 네 번째 int (기본적 값 영)

를받을

func<int, int, int>(1, 1, 1); 

작동하기 때문에 첫 번째 호출 전화

func<int, int>(1, 1, 1); 

,153,133,210는 int, int로서 설명되고 func() 기능하지 기본값과 제 int을 수신 한


sum(1, 1); // why doesn't this compile? 

같은 이유 : 파라미터 팩 그렇게 추론 될 수없는 최종 위치에 있지; 하지만 TintintRest...로 explicated 때문에

sum<int, int>(1, 1); 

작동합니다.


sum<int, int, int>(1, 1, 1); // why doesn't this compile while func<int, int, int>(1, 1, 1) does? 
Tint, intintRest...로 explicated되기 때문에 주먹 수준 호출이 작동

; 그러나 sum<int, int, int>(1, 1, 1)sum(rest...)으로 전화하십시오.이 경우에는 sum(1, 1)입니다. 그것은 Rest... 추론

// why are these compile? I only changed the order of the parameters compared to sum() 
sum2(1); 
sum2(1, 1); 
sum2(1, 1, 1); 

sum2()Rest... 마지막 위치에있는 매개 변수 팩 목록이 그렇게 될 수 있기 때문에


을 추론 할 수 있도록 마지막 위치에 있지 않습니다 (이며) 때문에 실패가 sum(1, 1)입니다.

관련 문제