Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we omit const on local variables in constexpr functions?

Tags:

People also ask

Is constexpr always const?

A constexpr variable must be initialized at compile time. All constexpr variables are const . A variable can be declared with constexpr , when it has a literal type and is initialized. If the initialization is performed by a constructor, the constructor must be declared as constexpr .

Is constexpr implicitly const?

In C++11, constexpr member functions are implicitly const.

Why is constexpr over const?

const can only be used with non-static member function 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 you modify constexpr?

A const int var can be dynamically set to a value at runtime and once it is set to that value, it can no longer be changed. A constexpr int var cannot be dynamically set at runtime, but rather, at compile time. And once it is set to that value, it can no longer be changed.


For example:

constexpr int g() { return 30; }    

constexpr int f()
{
    // Can we omit const?
    const int x = g();
    const int y = 10;

    return x + y;
}

Is there any point to ever declare local variables in a constexpr function with const?

Aren't constexpr functions with const local variables equivalent to those with no const?

In other words, does constexpr on a function impose (imply) const on its local variables?