[cfe-dev] [OpenCL patch] Vector literals are rvalues *updated*

John McCall rjmccall at apple.com
Wed Sep 28 10:29:38 PDT 2011


On Sep 28, 2011, at 7:01 AM, Anton Lokhmotov wrote:
>>>>> this should be an r-value:
>>>>> (int4) (3,2,1,0)
>>>>> and this should be an l-value:
>>>>> (int4) {3,2,1,0}
> 
> I take a little issue with this.  When the OpenCL dialect of C is used,
> vector literals must be treated as rvalues, no matter which syntax is used.

No, "OpenCL vector literals", which are spelled with parentheses, must be treated as rvalues because the OpenCL standard which defines them gives them those semantics.  Similarly, compound literals, which are spelled with braces, must be given the semantics of the language which defines them, which in this case is C99.  As an example, compound literals of vector type are not subject to the OpenCL restriction that the initializers must be constant.

Keep in mind that the extension at work here is not "allow compound literals of vector type".  The extension at work here is "permit elementwise list-initialization of vector types";  thus you can do something like this:
  int4 v = { 0, 1, 2, 3 };
It just happens that this also implies that you can do the same thing with compound literals.  But it doesn't change the rules for compound literals, and they do still need to be l-values, regardless of whether you, personally, think that's useless.

John.



More information about the cfe-dev mailing list