Even when the compiler knows that the else
block won't be entered, the code inside it must be syntactically valid. Consider what happens here when p
is double
:
if (current == i){
return p.el0;
} else {
return get_my(p.el1, i, current++);
}
get_my
gets instantiated and inside it the compiler sees return p.e10
with Pair
being double
.
The solution is overloading. Write another overload of my_get
that stops the recursion when Pair
is deduced for something else than my_pair
. This can be done with enable_if
idiom for example.
Even better, you could have an overload with my_pair<T1, T2>
that recurses, and another general template that does not. No enable_if
needed.