t****t 发帖数: 6806 | 1 C/C++对这个其实要求很严格的, 毕竟现在已经不是靠汇编写程序的时代, 编译器完成
了更多的工作,于是对程序有了更多的限制.
如我刚才所说,
const int i=5;
int * j=const_cast&i;
*j=6;
这个是undefined, 因为i在声明时就是const, 不管用什么方法, 都不能改它, 否则就
是undefined.
int i=6;
const int * j=&i;
int * k=const_castj;
*k=7;
这个是defined.
另一种要注意的情况:
float j;
int * i=static_cast&j;
i=10;
这个是undefined. 当然, 这种情况比较少见, 因为float和int并不非常compatible,
更多见的是这样:
int* x;
void* y;
void** z=static_cast(&x);
*z=y;
这个也是undefined, 而且因为void*和int*比较compatible, 有时可能会想要这样写.
事实上, 通过另一个类型A的 | P*****f 发帖数: 2272 | 2 的确应该严格按照语义编程。
楼主地代码加上volatile,也算是符合volatile qualifier得语义,虽然晦涩。
很多underfined bahavior 得代码都是在试探特定编译器的底线。除了for fun,是没啥
好处。
C/C++对这个其实要求很严格的, 毕竟现在已经不是靠汇编写程序的时代, 编译器完成
了更多的工作,于是对程序有了更多的限制.
如我刚才所说,
const int i=5;
int * j=const_cast&i;
*j=6;
这个是undefined, 因为i在声明时就是const, 不管用什么方法, 都不能改它, 否则就
是undefined.
int i=6;
const int * j=&i;
int * k=const_castj;
*k=7;
这个是defined.
另一种要注意的情况:
float j;
int * i=static_cast&j;
i=10;
这个是undefined. 当然, 这种情况比较少见, 因为float和int并不非常compatible,
更多见的是这样:
int*
【在 t****t 的大作中提到】 : C/C++对这个其实要求很严格的, 毕竟现在已经不是靠汇编写程序的时代, 编译器完成 : 了更多的工作,于是对程序有了更多的限制. : 如我刚才所说, : const int i=5; : int * j=const_cast&i; : *j=6; : 这个是undefined, 因为i在声明时就是const, 不管用什么方法, 都不能改它, 否则就 : 是undefined. : int i=6; : const int * j=&i;
| o***g 发帖数: 2784 | 3 你太有才了
【在 t****t 的大作中提到】 : C/C++对这个其实要求很严格的, 毕竟现在已经不是靠汇编写程序的时代, 编译器完成 : 了更多的工作,于是对程序有了更多的限制. : 如我刚才所说, : const int i=5; : int * j=const_cast&i; : *j=6; : 这个是undefined, 因为i在声明时就是const, 不管用什么方法, 都不能改它, 否则就 : 是undefined. : int i=6; : const int * j=&i;
| t****t 发帖数: 6806 | 4 you convert a const to non-const, *ONLY* when you know it *IS* indeed a non-
const.
you can NOT convert a volatile to non-volatile. Any such access is undefined. | S*****n 发帖数: 227 | 5
这里问个问题,如果把上面的static_cast换成reinterpret_cast,还undefined么?
【在 t****t 的大作中提到】 : you convert a const to non-const, *ONLY* when you know it *IS* indeed a non- : const. : you can NOT convert a volatile to non-volatile. Any such access is undefined.
| t****t 发帖数: 6806 | 6 doesn't matter what cast you use. you access it with "incompatible" access
path, you get undefined behaviour.
【在 S*****n 的大作中提到】 : : 这里问个问题,如果把上面的static_cast换成reinterpret_cast,还undefined么?
| S*****n 发帖数: 227 | 7 then what u mean here is type safety, right?
指针是万恶之源。haha..
【在 t****t 的大作中提到】 : doesn't matter what cast you use. you access it with "incompatible" access : path, you get undefined behaviour.
| t****t 发帖数: 6806 | 8 it's not exactly about type safety. you can use union to force a "compatible
" type, and signed/unsigned char is always compatible with every type. i
mentioned this because i feel the "compatible type" in C/C++ may not be
exactly like what you imagine.
it's for compiler's benefit so that they can do more aggressive optimization
. well, it's true pointers are evil, but as you have seen, references are
not much better.:)
【在 S*****n 的大作中提到】 : then what u mean here is type safety, right? : 指针是万恶之源。haha..
| D****g 发帖数: 2860 | 9
compatible
optimization
exactly. go for functional programming where you have referential
transparency.
【在 t****t 的大作中提到】 : it's not exactly about type safety. you can use union to force a "compatible : " type, and signed/unsigned char is always compatible with every type. i : mentioned this because i feel the "compatible type" in C/C++ may not be : exactly like what you imagine. : it's for compiler's benefit so that they can do more aggressive optimization : . well, it's true pointers are evil, but as you have seen, references are : not much better.:)
| t****t 发帖数: 6806 | | t****t 发帖数: 6806 | 11 hehe. people have different requirement. i don't have much experience with
functional programming, i think it's easier for programmer.
for type safety, actually C++ is already much much better than C. its type
system is less easy to mess up. however, it keeps the cast and added more
cast, which is the real root of evil. none of today's discussion will happen
without cast.
【在 D****g 的大作中提到】 : : compatible : optimization : exactly. go for functional programming where you have referential : transparency.
| D****g 发帖数: 2860 | 12 hehe, casting IS evil.
happen
【在 t****t 的大作中提到】 : hehe. people have different requirement. i don't have much experience with : functional programming, i think it's easier for programmer. : for type safety, actually C++ is already much much better than C. its type : system is less easy to mess up. however, it keeps the cast and added more : cast, which is the real root of evil. none of today's discussion will happen : without cast.
| t****t 发帖数: 6806 | 13 oh, 对的, 是不行
我随手写的, 不好意思 |
|