compiler tries to evaluate unreachable code after constexpr if

  • A+
Category:Languages

I've experimenting with C++17 lately and found this:

template<size_t i> void recurse() {     if constexpr(i == 0)         return;     return recurse<i - 1>(); } 

Trying to call recurse<4>(); will lead to

fatal error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) return recurse<i - 1>();

Adding an else fixes the error:

template<size_t i> void recurse() {     if constexpr(i == 0)         return;     else         return recurse<i - 1>(); } 

Is this a bug? Unfortunately I don't have access to another compiler than gcc 7.3.0 right now.

 


No: isn't a bug.

Both if constexpr and else are necessary.

In you first version

template<size_t i> void recurse() {     if constexpr(i == 0)         return;     return recurse<i - 1>(); } 

the recurse<i-1>() is compiled also when i == 0, so is generated recurse<-1>(), so is generated recurse<-2>(), etc.

You need the else to link the return recurse<i-1>() to if constexpr (i == 0) and avoid it's compilation when i == 0 ending the recursion.

You can try the second version removing constexpr

template<size_t i> void recurse() {     if (i == 0)         return;     else         return recurse<i - 1>(); } 

and you should get the same "template instantiation depth exceeds maximum of 900" recursion error.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: