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;
A constexpr integral value can be used wherever a const integer is required, such as in template arguments and array declarations. And when a value is computed at compile time instead of run time, it helps your program run faster and use less memory.
#define directives create macro substitution, while constexpr variables are special type of variables. They literally have nothing in common beside the fact that before constexpr (or even const ) variables were available, macros were sometimes used when currently constexpr variable can be used.
const can only be used with non-static member functions whereas constexpr can be used with member and non-member functions, even with constructors but with condition that argument and return type must be of literal types.
A call to a constexpr function produces the same result as a call to an equivalent non- constexpr function , except that a call to a constexpr function can appear in a constant expression. The main function cannot be declared with the constexpr specifier.
std::initializer_list<int> il = rand() ? std::initializer_list<int>{1}
: std::initializer_list<int>{1,2,3};
std::array<int, il.size()> a;
That's why.
A constexpr
member function is a function that can be executed within a constant expression, it doesn't necessarily yield a result that is a compile-time constant. For example:
struct S
{
int m;
constexpr int foo() const { return m; }
};
S s{rand()};
int j = s.foo(); // only known at run-time
constexpr S cs{42};
int arr[cs.foo()]; // compile-time constant
By writing std::array<int, il.size()> a;
you are claiming that il.size()
can be evaluated at compile time with a constant result, allowing template instantiation.
That's why both the initializer_list::size()
method and your il
variable need to be declared as constexpr
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With