dimanche 26 février 2017

Template parameters to constexpr

I am trying to pass a more "generic" const input parameter to a constexpr implementation for fibonacci. When I replace the template parameter with an int, things are hunky-dory again.

#include<iostream>
template <typename T>
constexpr auto fib_ce(T n) {
   return (n>1) ? fib_ce(n-1)+fib_ce(n-2) : 1;
}

int main() {
   std::cout<<fib_ce(4)<<"\n";
}

This is the error I get:

g++ -std=c++14 -o constexpr_fib constexpr_fib.cpp 
constexpr_fib.cpp:4:19: fatal error: recursive template instantiation exceeded maximum depth of 256
   return (n>1) ? fib_ce(n-1)+fib_ce(n-2) : 1;

              ^

How do I provide a template argument to a constexpr, that can take inputs like long, int, unsigned long, etc etc for this constexpr

Aucun commentaire:

Enregistrer un commentaire