Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 516
user3041565 Avatar asked Nov 27 '13 11:11

user3041565


People also ask

Why is constexpr necessary?

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.

Why use constexpr instead of define?

#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.

Can a member function be constexpr?

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.

Can a constexpr function call a non constexpr function?

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.


2 Answers

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
like image 151
dyp Avatar answered Oct 11 '22 12:10

dyp


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.

like image 35
Lorenzo Gatti Avatar answered Oct 11 '22 14:10

Lorenzo Gatti