dzada
dzada

Reputation: 5854

What is the difference between a static const and constexpr variable?

I understand that a constexpr variable can be used at compiletime. For a template, or static asser for instance.

But if I want to do that without constexpr I can with static const.

What is since C++11/14 introduced constexpr the difference between

constexpr int a = 3;
//AND
static const int a = 3;

Thank you!

Another way to see this question is which should I use?

Upvotes: 8

Views: 1377

Answers (1)

masoud
masoud

Reputation: 56479

The main difference that I know is, the value of constexpr must be known in compile-time while a const static can be assigned in run-time.

const static int x = rand();

Upvotes: 10

Related Questions