Document number: N4472

Ville Voutilainen
2015-04-10

constexpr goto

Abstract

Various users report they would like to do something like


for (auto x : a) {
  for (auto y : b) {
    if (cond)
      goto out;
  }
}
out:

and have it be allowed in constant expressions, but they can't, since goto isn't allowed in a constant expression. The users report that the technique is the most straightforward way to break out of multiple loops, and certain kinds of parser code naturally gravitate towards such techniques. The users also say they would prefer to allow such parser code to be eligible for constant expressions.

Rumination

We have rejected "labeled-break" proposals before, suggesting lambdas, goto, and some other techniques as reasonable work-arounds. We will have a proposal for constexpr lambdas, but gotos still can't be used in constant expressions. We treat our control statements unequally in this regard.

Whatever its faults, goto has valid and reasonable uses. Some of those uses are the most convenient way to express code for certain problems.

There is unsubstantiated hearsay according to which banning goto in constant expressions is more for taste reasons than technical reasons, meaning that supporting goto in constant expressions isn't particularly hard to implement. I can't say whether that's correct for implementations in general.

The ultimate question

Do we think the valid and reasonable uses of goto are important enough that we should support goto in constant expressions?