samedi 30 juillet 2016

Why I can't increment the parameter of the simple constexpr function?

Visual Studio 2015 Update 3.

I read the Programming. Principles and Practice Using C++ (second edition) by Bjarne Stroustrup. I learn the constexpr functions...

It works:

constexpr int get_value(int n) {
    return n + 1;
}

But I can't compile this (instead of the first variant):

constexpr int get_value(int n) {
    return ++n;
}

I get the error:

constexpr function return is non-constant

The n is the local variable for the get_value function. I.e. n variable changing doesn't influence to external code.

Why the second variant of the get_value function is wrong?

Aucun commentaire:

Enregistrer un commentaire