[cfe-dev] [RFD] Are enums allowed to capture local const variables?

Anna Welker via cfe-dev cfe-dev at lists.llvm.org
Thu Oct 17 10:00:36 PDT 2019

Hi all,

The following code snippet will compile with GCC, but not with LLVM:

constexpr unsigned g(const float *) { return 3; }
unsigned f() {
  const float x[] = {1.0};
  enum  { SIZE = g(x) };
  return SIZE;

At first glance, this looks fine to me, as the expression in the assignment is a constexpr function call with a const parameter.
Clang however errors with "reference to local variable 'x' declared in enclosing function".

I traced this down to the function getParentOfCapturingContextOrNull in SemaExpr.cpp, which will diagnose the variable reference uncapturable if it does not appear in a block literal, captured statement, or lambda expression.

My question is: Why does this list not contain enum declarations, at least if the declaration is local and the variable is in the same scope?

I tried to find a mention of this in the C++ standard, but did not succeed so far.
If this is a bug I'm happy to file it on bugzilla, but I first wanted to ask what the opinions are on whether this code should be valid or not.

Kind regards,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20191017/ef4118d6/attachment.html>

More information about the cfe-dev mailing list