# [C++,doc] vector conditional expression

Gerald Pfeifer gerald@pfeifer.com
Sat Jan 25 21:49:00 GMT 2014

```Hi Mark,

On Wed, 22 Jan 2014, Marc Glisse wrote:
> Gerald, are you ok with this version? Jason's approval is conditional

sorry for the delay.  I "killed" my notebook on the first day of
vaction in December by means of a few drops of water, and am still
catching up from weeks without connectivity more or less.

>> I think you had parsed it ok. In code terms:
>> size: sizeof(vec)
>> number of elements: sizeof(vec)/sizeof(vec)
>>
>> Now when the number of elements is fixed, saying that vectors have
>> the same (total) size or that they have elements of the same size is
>> equivalent, so any interpretation is fine.

Got it.

>> Ok. Like this then?
>>
>> +In C++, the ternary operator @code{?:} is available. @code{a?b:c}, where
>> +@code{b} and @code{c} are vectors of the same type and @code{a} is an
>> +integer vector with the same number of elements of the same size as @code{b}
>> +and @code{c}, computes all three arguments and creates a vector
>> +@code{@{a?b:c, a?b:c, @dots{}@}}.  Note that unlike in
>> +OpenCL, @code{a} is thus interpreted as @code{a != 0} and not @code{a < 0}.

In the above, would "with the same number of elements" be sufficient?

Or do the elements of @code{a} really need to be the same size as those
of @code{b} and @code{c} as "...of the same size..." seems to imply?

I _think_ I now understand everything, the above just makes me wonder
whether that is a false sense of security. ;-)

>> +As in the case of binary operations, this syntax is also accepted when
>> +one of @code{b} or @code{c} is a scalar that is then transformed into a
>> +vector. If both @code{b} and @code{c} are scalars and the type of
>> +@code{true?b:c} has the same size as the element type of @code{a}, then
>> +@code{b} and @code{c} are converted to a vector type whose elements have
>> +this type and with the same number of elements as @code{a}.
>>
>> (though arguably one could parse this to mean that the elements of a
>> have the same size as the whole vector b, but I am fine with ignoring
>> this)

Yeah, I'm not worried about that.

What puzzles me a bit is the part about the size of the type of
@code{true?b:c} being the same as the element type of @code{a}.

I would have expected that @code{a} could be a vector of bool, @code{b}
a long integer constant and @code{c} another long integer constant, but
apparently this is not covered (sizeof bool != size long)?

Gerald

```