Can constexpr function return pointer of local object?

  • A+
Category:Languages

A constexpr function is defined as (c++14)

A constexpr function must satisfy the following requirements:

  • it must not be virtual
  • its return type must be LiteralType each of its parameters must be LiteralType
  • there exists at least one set of argument values such that an invocation of the function could be an evaluated subexpression of a core constant expression (for constructors, use in a constant initializer is sufficient) (since C++14). No diagnostic is required for a violation of this bullet.

the function body must be either deleted or defaulted or contain any statements except:

  • an asm declaration
  • a goto statement
  • a statement with a label other than case and default
  • a try-block
  • a definition of a variable of non-literal type
  • a definition of a variable of static or thread storage duration
  • a definition of a variable for which no initialization is performed.

Now following func1 meets the requirement and compiles

constexpr int * func1 (int a) {   int b = 4;   return &b; } int main() {     constexpr int * a = func1(3);     int arr[*a];       std::cout << a << std::endl; } 

Now my question is how come func1 is constexpr. How does it know address of local variable at compile time?

I am using gcc 6.4.0


How does it know address of local variable at compile time?

It doesn't.

The third bullet in your quote is never satisfied:

There exists at least one set of argument values such that an invocation of the function could be an evaluated subexpression of a core constant expression (for constructors, use in a constant initializer is sufficient) (since C++14). No diagnostic is required for a violation of this bullet.

The compiler just doesn't complain about it because it's not required to, until you make it complain by trying to use func1 inside something that requires a correct constexpr function, for example:

std::array<int, func(3)> d; 

This won't compile and your compiler will tell you why.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: