conditional operator bug in c++?

Viewed 191

These demo functions should not be infinite loops in gcc because the conditional operator should only evaluate the active part.

They compile correctly in visual studio 2015 but give infinite recursion error compile error in g++ 6.3. What am I missing?

template <int n>
constexpr int infinite_loop_error(){
   return (n)  ? infinite_loop_error<n - 1>() : 0;
}

template <int n> 
constexpr int infinite_loop_error_2(){
   if (n) return infinite_loop_error_2<n - 1>(); 
   else   return 0;
}

void main() {
  infinite_loop_error<3>();
  infinite_loop_error_2<3>();
}
1 Answers
Related