vendredi 22 mai 2015

C++11 is it possible to force constexpr to be evaluated when it is used as a function parameter?

Suppose I have such code:

constexpr int foo(int a)
{
    return a * 2;
}
constexpr int bar(const char* str)
{
    return strlen(str);
}
int test(int value)
{
    // Do something
}
int main(int argc, char* argv[])
{
    // Case 1, evaluated at compile time
    const int value = foo(1);
    test(value);
    // Case 2, evaluated at run-time
    test(foo(1)); 

    // Case 3
    test(bar("Hello"));
}

In the case 1 the foo would be evaluated at compile time while in case 2 it would be evaluated at run-time. And even in the case 1 I have to keep the const keyword.

Is there any way to force the constexpr to be evaluated at compile time even it is not going to be assigned to a const?

Aucun commentaire:

Enregistrer un commentaire