Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a constexpr function contain a label?

This program:

constexpr void f() { x: ; }

is compiled by gcc, but clang says:

error: statement not allowed in constexpr function

So is this code valid?

like image 216
cigien Avatar asked Nov 16 '20 04:11

cigien


People also ask

Which is false about constexpr?

Short answer: static_assert(false) should never appear in a constexpr if expression, regardless of whether it's in a template function or whether it's in the discarded branch.

How do I know if a function is constexpr?

The easiest way to check whether a function (e.g., foo ) is constexpr is to assign its return value to a constexpr as below: constexpr auto i = foo(); if the returned value is not constexpr compilation will fail.

Can constexpr functions call non constexpr functions?

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.


1 Answers

As pointed out in a comment by Nathan Pierson, Clang is correct, and the code is ill-formed. According to the current working draft (which includes C++20), dcl.constexpr#3 says:

The definition of a constexpr function shall satisfy the following requirements:

...

its function-body shall not enclose

...

an identifier label,

...

...

like image 95
cigien Avatar answered Sep 21 '22 14:09

cigien