0

Paragraph §8.3.5/8 (emphasis mine):

If the type of a parameter includes a type of the form “pointer to array of unknown bound of T” or “reference to array of unknown bound of T,” the program is ill-formed. Functions shall not have a return type of type array or function, although they may have a return type of type pointer or reference to such things. There shall be no arrays of functions, although there can be arrays of pointers to functions.

Bullet points (5.2.1) and (5.2.1.1) of §8.5.3/5 (emphasis is mine):

(5.2.1) — If the initializer expression

(5.2.1.1) — is an xvalue (but not a bit-field), class prvalue, array prvalue or function lvalue and “cv1 T1” is reference-compatible with “cv2 T2”, or

If I'm wrong with my assumption (in the title), I would appreciate having an example producing a prvalue array.

Belloc
  • 6,318
  • 3
  • 22
  • 52
  • I'm confused, isn't the second section you're quoting dealing with references? Which things do you think are incompatible? – mbgda Jan 14 '15 at 19:29
  • @mbgda The first statement says that a function doesn't return an array and the second statement mentions the term `array prvalue`, which seem to be incompatible. But Quentin showed another possibility for producing an array prvalue. – Belloc Jan 14 '15 at 19:33
  • @mbgda `array prvalue is effectively just a reference to an array` This is not correct. An lvalue reference is an lvalue and an rvalue reference is an xvalue. Thus, references are never prvalues. – Belloc Jan 14 '15 at 19:56
  • I don't think you really read that in context. I didn't mean it's a reference in language terms (as in `&`) - but it's a reference in the meaning that the memory allocated shouldn't have to be copied over, the compiler is able to generate code that can point to the already-created object after you transfer ownership - kind of like how copy elision works for function returns. However you raise a good point that the comment is a little ambiguous there - thanks for pointing it out. – mbgda Jan 14 '15 at 19:58

1 Answers1

5
int main() {
    using Arr = int[];
    auto&& r = Arr{1, 2, 3, 4, 5};
    return 0;
}

Here r binds to a prvalue of array type.

Quentin
  • 62,093
  • 7
  • 131
  • 191
  • That was clever. Thanks. – Belloc Jan 14 '15 at 19:31
  • Quick question. That isn't technically a prvalue, is it? Doesn't a prvalue specifically refer to an object that does not exist except as an anonymous temporary rvalue, such as a function return value? Edit - nevermind, misread it. I see that is *is* an anonymous temporary. Leaving my description about prvalue though since it may be helpful to others. – mbgda Jan 14 '15 at 19:48
  • One more thing to expand on this... this doesn't exactly (or at least completely) address OP's question. You could create `r` in a function and try to return it but you're going to be returning a reference to a temporary value in this case, so it doesn't *quite* address how a valid array prvalue can be returned from a function. [**This post**](http://stackoverflow.com/a/27281568/4421195) addresses how an array can be returned as a subobject prvalue. – mbgda Jan 14 '15 at 20:34
  • 1
    @mbgda The question wasn't how to return an array from a function (it's explicitly disallowed), but if not being able to do so contradicted the existence of array prvalues altogether. Hence, another way of getting an array prvalue. – Quentin Jan 15 '15 at 14:49
  • I see what you're saying. I didn't realize OP was questioning how an array prvlaue could exist in the first place - thanks for clarifying (+1). I was under the impression OP was asking how an array prvalue can be returned from a function - which (as you said) it cannot. If it's part of an non-array aggregate prvalue then it works, but of course at that point the prvalue is not of type array so its a different situation. – mbgda Jan 15 '15 at 14:58