Q:

Branch in variadic template

I don't understand why this doesn't get compiled:
#include <iostream>

template<int I1,int ...Is>
int getProdSeq() {

    if(sizeof...(Is)==0)
        return I1;
    else
        return I1*getProdSeq<Is...>();
}

int main() {

    int i = getProdSeq<9,7,8>();
    std::cout<<i<<std::endl;
}

In the last recursive call to the function, ...IS should be empty, so the first if-branch should be taken. The compiler also confirms this (that ...IS is empty):
main.cpp: In instantiation of 'int getProdSeq() [with int I1 = 8; int ...Is = {}]':
But the compiler also spits out following error:
main.cpp:5:5: note:   template argument deduction/substitution failed:
main.cpp:10:36: note:   couldn't deduce template parameter 'I1'
Well this error would make sense if the function was indeed called with an empty parameter-pack, but this shouldn't be the case as I circumvent this problem with the first if-statement. Why is the compiler still checking the else branch?

A:

If in instantiating
 template<int I1,int ...Is>
 int getProdSeq() {

The ...Is parameter pack is empty, then the following call:
    getProdSeq<Is...>();

Instantiates getProdSeq<> which, of course does not exist. Just because the preceding if statement will never execute it, doesn't mean that it still must be compilable. The result of a template expansion must be valid C++ code, and this is not. Just because the if statement's else clasuse will never get executed doesn't change the fact that whatever's inside else must still be valid C++ code.
One way to do this is to use two templates:
#include <iostream>

template<int I1>
int getProdSeq() {

       return I1;
}

template<int I1,int I2, int ...Is>
int getProdSeq() {

        return I1*getProdSeq<I2, Is...>();
}

int main() {

    int i = getProdSeq<9,7,8>();
    std::cout<<i<<std::endl;
}

