Conditional (ternary) operator
От: saf_e  
Дата: 15.10.13 13:58
Оценка:
Есть код:

std::vector<int> _1, _2;
(_1.empty() ? _1 : _2).push_back(3);
(_1.empty() ? _1 : _2).push_back(3);


По факту выполнения имеем значение в обоих векторах. Т.к. всегда считал что должна возвращаться копия полез читать мануал:

$5.16:

6. Lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are performed
on the second and third operands. After those conversions, one of the following shall hold:
— The second and third operands have the same type; the result is of that type. If the operands have
class type, the result is a prvalue temporary of the result type, which is copy-initialized from either
the second operand or the third operand depending on the value of the first operand.


Если я правильно понимаю, то стандарт со мной согласен. Или я что-то упустил?
Re: Conditional (ternary) operator
От: Abyx Россия  
Дата: 15.10.13 14:11
Оценка:
Здравствуйте, saf_e, Вы писали:

_>
_>(_1.empty() ? _1 : _2).push_back(3);
_>


в данном случае операнды это ссылки
In Zen We Trust
Re[2]: Conditional (ternary) operator
От: saf_e  
Дата: 15.10.13 14:12
Оценка:
Здравствуйте, Abyx, Вы писали:

A>Здравствуйте, saf_e, Вы писали:


_>>
_>>(_1.empty() ? _1 : _2).push_back(3);
_>>


A>в данном случае операнды это ссылки


Напрашивается вопрос почему?
Re: Conditional (ternary) operator
От: watchmaker  
Дата: 15.10.13 14:13
Оценка: 1 (1) +1
Здравствуйте, saf_e, Вы писали:

_>$5.16:


_>6.


_>Если я правильно понимаю, то стандарт со мной согласен. Или я что-то упустил?


Да, упустил пункты 1–5. Конкретно нужно смотреть на четвёртый:

4 If the second and third operands are lvalues and have the same type, the result is of that type and is an lvalue

Re[2]: Conditional (ternary) operator
От: saf_e  
Дата: 15.10.13 14:15
Оценка:
Здравствуйте, watchmaker, Вы писали:

W>Здравствуйте, saf_e, Вы писали:


_>>$5.16:


_>>6.


_>>Если я правильно понимаю, то стандарт со мной согласен. Или я что-то упустил?


W>Да, упустил пункты 1–5. Конкретно нужно смотреть на четвёртый:

W>4 If the second and third operands are lvalues and have the same type, the result is of that type and is an lvalue


Спасибо
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.