Quick Q: When do you need to declare a variable constexpr? -- StackOverflow

Quick A: When you want to use the variable in a way that requires its value to be known at compile time.

Here's a short nugget that helps demonstrate the meaning of constexpr:

Why is constexpr required even though member function is constexpr?

The following does not compile unless I put constexpr before initializer_list:

constexpr std::initializer_list<int> il = {
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10
};
std::array<int, il.size()> a;

But initializer_list size is constexpr:

constexpr size_type size() const;

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.