Re[3]: Deep C++. Operation: static_cast. Перевод - Igor Sukh
От: SergioSV Россия  
Дата: 01.04.09 11:17
Оценка:
Здравствуйте, Masterkent, Вы писали:

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


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


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


С этим вопросом вроде разобрался:

1. Исходя из стандарта 98-года получаем (5.2.9/10):

An rvalue of type "pointer to cv void" can be explicitly converted to a pointer to object type. 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.


Т.е., выходит, что преобразование "(возможно cv-квалифицированный) void * в любой T *" не запрещено.

2. Исходя из Working Draft, Standard for Programming Language C++ (2005-10-19), получаем (5.2.9/11):

An rvalue of type “pointer to cv1 void” can be converted to an rvalue of type “pointer to cv2 T,” where T is an object type
and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1. A value of type pointer to object converted
to “pointer to cv void” and back, possibly with different cv-qualification, shall have its original value. [ Example:
T* p1 = new T;
const T* p2 = static_cast < const T*>( static_cast < void *>( p1 ));
bool b = p1 == p2 ; / / b will have the value true.
—end example ]


Т.е., преобразование "(возможно cv-квалифицированный) void * в любой T *" разрешено, но с оговорками:

An rvalue of type “pointer to cv1 void” can be converted to an rvalue of type “pointer to cv2 T,” where T is an object type
and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1.

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