15

The first snippet compiles without any warnings (live example):

#include <iostream>

struct A {
  constexpr A(): i(5){}
  constexpr operator int() { return 5; }
  int i;
};

int main() {
    A a;
    int b[a]{ 0, 1, 2, 3, 4 };
    std::cout << b[4] << '\n';
}

Now alter the above snippet by returning i in the conversion operator (live example):

constexpr operator int() { return i; }

GCC warns that b is a VLA.

To me, both variants seem to conform to paragraph §5.19 [expr.const]/3 in C++14.

2 Answers 2

17

You're performing an l-t-r conversion on i, but for [expr.const]/(2.7) not to be violated here, (2.7.3) must be applicable:

enter image description here

(2.7.1) concerns complete objects, (2.7.2) talks about string literals and (2.7.4) is about objects whose lifetime began within the evaluation of the expression - inapplicable since a's declaration precedes b's.

Define a as constexpr and the code is compliant.


A little addendum to clarify what the standard says: The expression inside the brackets must be a converted constant expression of type std::size_t ([dcl.array]/1), which is defined in [expr.const]/4 as

A converted constant expression of type T is an expression, implicitly converted to type T, where the converted expression is a constant expression and […requirements that are met…]

Thus, really, the standard is interested in whether or not

constexpr std::size_t s = a; 

would be valid. Which it isn't, for the aforementioned reasons - trying to use a subobject of a previously defined, non-constexpr object.

6
  • Why does a have to undergo an lvalue-to-rvalue conversion in this case? I'm asking this, because the definition of a converted constant expression in §5.19/3 (N4140) doesn't seem to require such a conversion from a in the declaration int b[a]{ 0, 1, 2, 3, 4 };.
    – Ayrosa
    Dec 21, 2015 at 14:30
  • @Ayrosa Well, a never has to undergo one, but it's conversion operator must be called in order to convert it to std::size_t. (a is a converted constant expression of type std::size_t inside the brackets)
    – Columbo
    Dec 21, 2015 at 14:36
  • 1
    It took me a while to really grasp what you said and highlighted in your answer. Definitely, it's not an easy task for one, to fully understand the Standard. Great answer (+1).
    – Ayrosa
    Dec 21, 2015 at 15:04
  • @Ayrosa id-expression a has the class type A, but because of the bound of an array must be specified by a converted constant expression of type std::size_t, the implementation tries to find implicit conversion sequence to convert it to the destination type. See [conv] 4\3, 6. It finds user-defined conversion function constexpr operator int (), which is then used to initialize the temporary of type std::size_t, which value would be used as the bound of the array. But! Dec 21, 2015 at 15:11
  • 1
    @Ayrosa But! The resulting full-expression is not a core constant expression because, though, during that initialization, implied object argument a (see [over.match.conv] 13.3.1.5\2) is used as an lvalue (there is no l-t-r conversion for it), the conversion function, you defined, evaluate l-t-r for a subobject of a, which is not acceptable for a core constant expression, because neither the life-time of a begins within the evaluation of the considered full-expression, nor etc... Dec 21, 2015 at 15:16
4

Arrays size must be compile-time constants, but in the second example the initialization of A::i doesn't happen until run-time.

2
  • @YSC Yes, but the variable a in the main function is not. Dec 21, 2015 at 14:10
  • 1
    And this is the real warning source, isn't it?
    – YSC
    Dec 21, 2015 at 14:11

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.