Re[2]: Deep C++. Operation: static_cast. Перевод - Igor Sukh
От: Masterkent  
Дата: 01.04.09 09:55
Оценка: 5 (2) +1
Здравствуйте, SergioSV, Вы писали:

SSV>Исходя из статьи можно предположить то, что любой тип можно избавить от cv-модификаторов с использованием одного лишь static_cast


Избавить-то можно, но ценой undefined behavior:

const volatile int t0 = 5;
const volatile int *t1 = &t0;
int *t2 = *static_cast<int **>(static_cast<void *>(&t1));
int &t3 = *t2;

Формально такая цепочка преобразований не подпадает под правило во втором предложении 5.2.9/10:

A value of type pointer to object converted to “pointer to cv void” and back to the original pointer type will have its original value

а значит, это подпадает под второе предложение 1.3.12:

Undefined behavior may also be expected when this International Standard omits the description of any explicit definition of behavior.

Также не следует забывать про 7.1.5.1/7:

If an attempt is made to refer to an object defined with a volatile-qualified type through the use of an lvalue with a non-volatile-qualified type, the program behaviour is undefined

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