Skip to content

Instantly share code, notes, and snippets.

@VitaminaCPP
Created September 9, 2014 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VitaminaCPP/896c5d441c1290c0f271 to your computer and use it in GitHub Desktop.
Save VitaminaCPP/896c5d441c1290c0f271 to your computer and use it in GitHub Desktop.
template <unsigned int ITERACION> struct Fibonacci
{
static const unsigned int valor =
Fibonacci<ITERACION - 1>::valor +
Fibonacci<ITERACION - 2>::valor;
};
template <> struct Fibonacci<1>
{
static const unsigned int valor = 1;
};
template <> struct Fibonacci<0>
{
static const unsigned int valor = 0;
};
unsigned int diez() { return 10; }
// Los valores de plantilla necesitan ser constantes
// en tiempo de compilacion:
auto v1 = Fibonacci<10>::valor;
// Error de compilacion: diez() no es una constante
// en tiempo de compilacion
auto v2 = Fibonacci<diez()>::valor;
// La cantidad de elementos debe ser una constante
// en tiempo de compilacion:
int valores1[55] = { 0 };
// Error de compilacion: la variable v1 no
// es una constante en tiempo de compilacion:
int valores1[v1] = { 0 };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment