6

When a range based for loop is used to iterate over an array, without binding a reference to each element, does this constitute an ODR-use of the array?

Example:

struct foo {
    static constexpr int xs[] = { 1, 2, 3 };
};

int test(void) {
    int sum = 0;
    for (int x : foo::xs) // x is not a reference!
        sum += x;
    return sum;
}

// Definition, if needed
///constexpr foo::xs;

Is the definition of foo::xs necessary?

While this code, and variations of it, appear to work fine, that doesn't mean the definition is never necessary. Lack of a definition of an ODR-used variable rarely produces a diagnostic, since the variable could be defined in another translation unit. A linker error is the usual result, but it's quite possible to not get the error if the compiler is able to optimize away every use, which is what happens to the above code. The compiler effectively reduces test() to return 6;.

Binding a reference to an element would be an ODR-use, but that isn't done.

I was under impression that subscripting an array was not ODR-use in C++14 or later. But the range based for is not exactly subscripting.

In C++17, I believe this example avoids the problem because constexpr class data members are implicitly inline. And thus the declaration in the class also serves to define xs and an additional namespace scope definition isn't needed to satisfy ODR.

Some additional versions of the same question:

What if we use std::array?

constexpr std::array<int, 3> xs = { 1, 2, 3 };

What if we avoid the range based for?

for (int i = 0; i < foo::xs.size(); i++) sum += foo::xs[i];
5
  • 2
    In C++14 a range based for loop has auto && __range = range-expression ; in it, so I believe that makes it ODR used. May 26, 2022 at 3:42
  • Wrapping the array in a struct is a red herring. The compiler must know the size of the array to be used in a range (or other structured binding) context. This does not necessarily match ODR-use, as you can declare the array as extern, supplying only its size but populating it in another TU. The std::array is a different case: it provides a .size() member.
    – Dúthomhas
    May 26, 2022 at 3:44
  • 2
    I don't see anything in N4140 (the Standard draft just before official C++14) suggesting "subscripting an array is not ODR-use". The built-in X[Y] means *(X+Y) causing the array-to-pointer conversion, which is generally an ODR-use of an array variable.
    – aschepler
    May 26, 2022 at 4:17
  • 1
    @aschepler I'm referring to what was in defect report 1926 and appears in §[basic.def.odr] as "If E is a subscripting operation with an array operand, the set contains the potential results of that operand." In this case, E might be foo:xs[i], which is converted to an rvalue, and so isn't odr-used.
    – TrentP
    May 26, 2022 at 8:10
  • @Dúthomhas the problem can't exist without putting the constexpr variable in class scope, i.e. wrapped in a struct. If it was in namespace scope, then it would be a definition. In class scope, it's possible to declare and initialize a constexpr variable without defining it. How can a constexpr variable be extern?
    – TrentP
    May 26, 2022 at 8:16

1 Answer 1

2

Is the definition of foo::xs necessary?

Yes, because as NathanOliver points out in the comments, a reference is implicitly bound to foo::xs by the range-based for loop. When you bind a reference to an object, the object is odr-used. The same would occur if an std::array were used rather than a raw array.

What if we avoid the range based for?

Well, if you use a raw array and get its size using a technique that doesn't require binding a reference to it, then you can avoid providing a definition:

for (int i = 0; i < sizeof(foo::xs)/sizeof(foo::xs[0]); i++) {
    sum += foo::xs[i];
}

In this case, the references inside sizeof are not odr-uses because they are unevaluated, and foo::xs is an element of the set of potential results of foo::xs[i]; this latter expression is of non-class type and immediately undergoes an lvalue-to-rvalue conversion, so it does not odr-use foo::xs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.