由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 有关objec access path的问题
相关主题
conversion between const to nonconstC++ storage classes and qualifiers
what's the meaning of const_cast如果把一个class里面的一个instance declare volatile
请问一个多线程与volatile关键字的问题。问一个volatile和memcpy一起用的问题
问个C++编译器如何处理函数内的static 变量参加scala days的唯一感受
thread-safe singleton implementationAngular 2像Python 3一样流行不动的可能性多大?
关于const和volatile修饰变量或指针的问题用volatile退出线程对不对?(C++)
A C++ questionEmbedded C 编程问题求助
看了这篇文章,脑子有点不够用了About volatile in C
相关话题的讨论汇总
话题: int话题: const话题: cast话题: undefined话题: compatible
进入Programming版参与讨论
1 (共1页)
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
10
这个是我typo了, 应该是*i=10
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, 对的, 是不行
我随手写的, 不好意思
1 (共1页)
进入Programming版参与讨论
相关主题
About volatile in Cthread-safe singleton implementation
volatile关于const和volatile修饰变量或指针的问题
关于signal handlerA C++ question
[合集] Java怎么不能用新的版本编译?看了这篇文章,脑子有点不够用了
conversion between const to nonconstC++ storage classes and qualifiers
what's the meaning of const_cast如果把一个class里面的一个instance declare volatile
请问一个多线程与volatile关键字的问题。问一个volatile和memcpy一起用的问题
问个C++编译器如何处理函数内的static 变量参加scala days的唯一感受
相关话题的讨论汇总
话题: int话题: const话题: cast话题: undefined话题: compatible