Question Details

No question body available.

Tags

c++ language-lawyer

Answers (1)

Accepted Answer Available
Accepted Answer
January 28, 2026 Score: 5 Rep: 48,524 Quality: Expert Completeness: 50%

Is this order guaranteed by the C++ standard?

No, the order is unspecified. This is because as per expr.prim.lambda.capture#15 the initializations of the non-static data members corresponding to the captures(by copy) is explicitly left unspecified:

When the lambda-expression is evaluated, the entities that are captured by copy are used to direct-initialize each corresponding non-static data member of the resulting closure object, and the non-static data members corresponding to the init-captures are initialized as indicated by the corresponding initializer (which may be copy- or direct-initialization). (For array members, the array elements are direct-initialized in increasing subscript order.) These initializations are performed in the (unspecified) order in which the non-static data members are declared.


Note that this is a consequence of the fact that the order in which the non-static members corresponding to the capture(s) are declared, is itself unspecified as per expr.prim.lambda.capture#10:

For each entity captured by copy, an unnamed non-static data member is declared in the closure type. The declaration order of these members is unspecified. The type of such a data member is the referenced type if the entity is a reference to an object, an lvalue reference to the referenced function type if the entity is a reference to a function, or the type of the corresponding captured entity otherwise. A member of an anonymous union shall not be captured by copy.

This means that the implementations are allowed to use any order they see fit and they're not required to document that behavior. See Unspecified and implementation-defined behavior.


Also note that there is a proposal P3847R0 that aims to fix the ordering to be left-to-right.