jeudi 25 juin 2020

Why constexpr can only use to initialize constexpr var? [duplicate]

I can't understand what is constexpr at all, why a constexpr function use to initialize non-const var not computed result while compile, I think constexpr variable is just a var need initialize by constexpr function, but why it doesn't work on other var type?

#include <iostream>
#include <ctime>

constexpr long long fib(int n)
{
    return n > 1 ? fib(n - 2) + fib(n - 1) : n;
}

int main()
{
    const time_t start = clock();
    long long a = fib(25);
    std::cout << clock() - start << std::endl; //result is 6 on my computer
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire