Why declare int constexpr

While looking at the C ++ 11 tutorial related to isocpp.org I noticed something:

constexpr int windowWidth{800}, windowHeight{600};

What is the point of declaring these intvariables as constexpr, and not just const?

+4
source share
2 answers

Good video Vittorio!

The following is a brief description of the difference between an ad int constand constexpr:

int get_int();  // Some run time function that returns int

template <int N>  // example use requiring a compile time int
struct test {};

const     int w = get_int();  // initialized at run time
const     int x = 5;          // initialized at compile time
constexpr int y = get_int();  // error, can not initialize at compile time
constexpr int z = 6;          // initialized at compile time

int
main()
{
    test<w> tw;  // error, w is not a compile time constant
    test<x> tx;  // ok, x is a compile time constant
    test<y> ty;  // error, there is no compile time constant named y
    test<z> tz;  // ok, z is a compile time constant
}

constexpr, , , . const, , , .

const int, ( , const int), , const int .

constexpr int, , , , . , .

<Disclaimer>

Kerrek SB , " " . , . , " " " ", - , 5.19 " " [expr.const] .

, ([basic.start.init]/p2).

, , , , , , , .

</Disclaimer>

+5

.

.

constexpr . const .

, undefined . . DyP.

++ 11, , , , const, constexpr.

constexpr const constexpr. constexpr int windowWidth;, 100% , , .


, constexpr , constexpr constexpr /.

+4

All Articles