avatar
主流机i5还是Ryzen 5?# Hardware - 计算机硬件
c*e
1
这个const_cast什么意思?是说a不是常数了吗?
const_cast(a) = other.a;
*****************
class A{
const int a;
public:
A():a(0){};
A(int m_a):a(m_a){};
A& operator =(const A &other);
};
A & A::operator =(const A &other)
{
const_cast(a) = other.a;
return *this;
}
avatar
h*b
2
$700这个价位, 感觉AMD毫无优势啊.
更低价位, 还有G4560这样的$60神器。
显卡RX 480/GTX 1060, 250GB SSD, 8GB DDR4 内存.
1080p High/Ultra玩所有主流游戏.
avatar
m*l
3
turn const-ness on AND off.
C++'s gay hack.

【在 c**********e 的大作中提到】
: 这个const_cast什么意思?是说a不是常数了吗?
: const_cast(a) = other.a;
: *****************
: class A{
: const int a;
: public:
: A():a(0){};
: A(int m_a):a(m_a){};
: A& operator =(const A &other);
: };

avatar
i*a
4
无腦 ryzen 吧,价格便宜

【在 h******b 的大作中提到】
: $700这个价位, 感觉AMD毫无优势啊.
: 更低价位, 还有G4560这样的$60神器。
: 显卡RX 480/GTX 1060, 250GB SSD, 8GB DDR4 内存.
: 1080p High/Ultra玩所有主流游戏.

avatar
l*a
5
cast away the constness
otherwise,you can't change the value of a

【在 c**********e 的大作中提到】
: 这个const_cast什么意思?是说a不是常数了吗?
: const_cast(a) = other.a;
: *****************
: class A{
: const int a;
: public:
: A():a(0){};
: A(int m_a):a(m_a){};
: A& operator =(const A &other);
: };

avatar
h*b
6
CPU价格好像差不多?

【在 i****a 的大作中提到】
: 无腦 ryzen 吧,价格便宜
avatar
c*e
7
这个说法不错。是不是先去掉constness,赋值后再弄回来。最终还是const,是不是?

【在 m********l 的大作中提到】
: turn const-ness on AND off.
: C++'s gay hack.

avatar
m*g
8
除了八核的。其他的产品两家性价比就差不多。
哪个有deal买哪个。
g4560配1060会有瓶颈。1060完全发挥需要4核3.5.

【在 h******b 的大作中提到】
: $700这个价位, 感觉AMD毫无优势啊.
: 更低价位, 还有G4560这样的$60神器。
: 显卡RX 480/GTX 1060, 250GB SSD, 8GB DDR4 内存.
: 1080p High/Ultra玩所有主流游戏.

avatar
c*e
9
是这样,但最后a还是常数吗?

【在 l*****a 的大作中提到】
: cast away the constness
: otherwise,you can't change the value of a

avatar
k*0
10
内存要16G

【在 h******b 的大作中提到】
: $700这个价位, 感觉AMD毫无优势啊.
: 更低价位, 还有G4560这样的$60神器。
: 显卡RX 480/GTX 1060, 250GB SSD, 8GB DDR4 内存.
: 1080p High/Ultra玩所有主流游戏.

avatar
f*i
11
tried
const int a = 2;
const_cast(a) = 3;
if print out:
std::cout << a;
shows 2;
std::cout << const_cast(a);
shows 3;
why??
Thanks.
avatar
z*e
12
搞了个100块的i7专门娱乐机用
一个月了都没来得及装上

【在 i****a 的大作中提到】
: 无腦 ryzen 吧,价格便宜
avatar
S*I
13
The result is undefined behavior. When a variable is declared const, the
compiler can store the variable anywhere it wants (may be read-only memory
location), when you cast away the constness of this variable and make
assignment, the compiler is forced to create a variable on the stack. So in
your example, a and const_cast(a) could be two different variables and
have different memory location.
If you run the following code, you can see the difference:
const int a = 2;
int b = &const_cast(a);
b = 3;
cout << a << endl;
cout << b << endl;

【在 f*****i 的大作中提到】
: tried
: const int a = 2;
: const_cast(a) = 3;
: if print out:
: std::cout << a;
: shows 2;
: std::cout << const_cast(a);
: shows 3;
: why??
: Thanks.

avatar
d*e
14

对,AMD的还有老酒效应:时间越久,优化越多从而性能更好。而且以后还可以CPU
drop in升级到Zen+。

【在 i****a 的大作中提到】
: 无腦 ryzen 吧,价格便宜
avatar
f*i
15
Thanks, tested following code
const int a = 2;
std::cout << &a << std::endl;

int* b = &(const_cast(a));
*b = 3;
std::cout << &a << std::endl;
std::cout << b << std::endl;
std::cout << a << std::endl;
std::cout << *b << std::endl;
result is:
004EF808
004EF808
004EF808
2
3
super confused now.

in
and

【在 S**I 的大作中提到】
: The result is undefined behavior. When a variable is declared const, the
: compiler can store the variable anywhere it wants (may be read-only memory
: location), when you cast away the constness of this variable and make
: assignment, the compiler is forced to create a variable on the stack. So in
: your example, a and const_cast(a) could be two different variables and
: have different memory location.
: If you run the following code, you can see the difference:
: const int a = 2;
: int b = &const_cast(a);
: b = 3;

avatar
k*0
16
电子产品时间长了就是垃圾

【在 d********e 的大作中提到】
:
: 对,AMD的还有老酒效应:时间越久,优化越多从而性能更好。而且以后还可以CPU
: drop in升级到Zen+。

avatar
S*I
17
I bet if you run a debugger for this example, you will see that both a and *
b have value 3 after "*b = 3", but the output of "std::cout << a" is still 2.
The reason is when compiler sees "std::cout << a", since a is declared as
const and initialized to 2, it may replace this statement with "std::cout <<
2". The compiler never bothers to check whether the value of a has been
modified before "std::cout << a". This is a valid optimization and many
compilers do it.
Change you code to the following and you can see this optimization:
const int a = 2;
std::cout << &a << std::endl;
int* b = &(const_cast(a));
*b = 3;
int c = a;
std::cout << &a << std::endl;
std::cout << b << std::endl;
std::cout << a << std::endl;
std::cout << *b << std::endl;
std::cout << c << std::endl;
Try to modify a reference to a constant by casting away its constness is
undefined behavior, which means anything can happen. In this example it
could be that a is stored on the stack, hence b and &a are the same; but
there is no guarantee that this behavior will remain the same in a different
program or when a different compiler is used.

memory

【在 f*****i 的大作中提到】
: Thanks, tested following code
: const int a = 2;
: std::cout << &a << std::endl;
:
: int* b = &(const_cast(a));
: *b = 3;
: std::cout << &a << std::endl;
: std::cout << b << std::endl;
: std::cout << a << std::endl;
: std::cout << *b << std::endl;

avatar
f*i
18
Thanks,
tried it, c is still 2, but I think I get the idea, it may still because of
optimization.
Basically in my understanding now, it's not that safe to use const_cast. or
there maybe a condition that is safe?

*
2.
<<

【在 S**I 的大作中提到】
: I bet if you run a debugger for this example, you will see that both a and *
: b have value 3 after "*b = 3", but the output of "std::cout << a" is still 2.
: The reason is when compiler sees "std::cout << a", since a is declared as
: const and initialized to 2, it may replace this statement with "std::cout <<
: 2". The compiler never bothers to check whether the value of a has been
: modified before "std::cout << a". This is a valid optimization and many
: compilers do it.
: Change you code to the following and you can see this optimization:
: const int a = 2;
: std::cout << &a << std::endl;

avatar
S*I
19
As long as you don't try to modify a declared constant, it is fine. For
example:
void foo(int* p){
//do something with p, but do not change what p points to
}
int main (){
const int a = 2;
const int* b = &a;
foo(b); //error! foo expects int*, not const int*
int* c = const_cast(b);
foo(c); //this is OK
}
Many legacy C codes contain functions like foo, so it is necessary to use
const_cast in these cases.
Another example:
int main(){
int a = 2;
const int* b = &a;
int* c = const_cast(b);
*c = 3;
std::cout << a << endl;
}
In this case, although b points to const int type, but a is not declared as
const, so it is valid to modify a by modifying *c.

of
or

【在 f*****i 的大作中提到】
: Thanks,
: tried it, c is still 2, but I think I get the idea, it may still because of
: optimization.
: Basically in my understanding now, it's not that safe to use const_cast. or
: there maybe a condition that is safe?
:
: *
: 2.
: <<

avatar
f*i
20
Thanks.

【在 S**I 的大作中提到】
: As long as you don't try to modify a declared constant, it is fine. For
: example:
: void foo(int* p){
: //do something with p, but do not change what p points to
: }
: int main (){
: const int a = 2;
: const int* b = &a;
: foo(b); //error! foo expects int*, not const int*
: int* c = const_cast(b);

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。