Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 寄存器缓存是什么?它与常量变量有什么关系?_C++_Constants_Compiler Optimization_Cpu Registers_Aliasing - Fatal编程技术网

C++ 寄存器缓存是什么?它与常量变量有什么关系?

C++ 寄存器缓存是什么?它与常量变量有什么关系?,c++,constants,compiler-optimization,cpu-registers,aliasing,C++,Constants,Compiler Optimization,Cpu Registers,Aliasing,发件人: 即使该语言禁止const_cast,避免在const成员函数调用中刷新寄存器缓存的唯一方法是解决别名问题(即,证明没有指向对象的非const指针) 什么是寄存器缓存,在const成员函数调用中刷新它意味着什么?这里“寄存器缓存”意味着编译器将值存储在寄存器中 调用const成员函数不应更改任何成员变量的值,因此,如果其中一些变量存储在寄存器中,则当函数返回时,这些值仍然有效 我想这不是一个非常重要的优化。我认为它是在谈论这样的事情: class A; class B { public

发件人:

即使该语言禁止
const_cast
,避免在
const
成员函数调用中刷新寄存器缓存的唯一方法是解决别名问题(即,证明没有指向对象的非
const
指针)

什么是寄存器缓存,在
const
成员函数调用中刷新它意味着什么?

这里“寄存器缓存”意味着编译器将值存储在寄存器中

调用
const
成员函数不应更改任何成员变量的值,因此,如果其中一些变量存储在寄存器中,则当函数返回时,这些值仍然有效


我想这不是一个非常重要的优化。

我认为它是在谈论这样的事情:

class A;

class B {
public:
    A * m_a;
    B(A * a) : m_a(a) {}
}; 

Class A {
public:
    int m_num;
    A(int num=0) : m_num(num) {}
    void DoSomethingConst(B * someB) const;
};

void SomeOtherFunction()
{
   A myA;
   B myB(&myA);

   //do something with myA.m_num (1)
   myA.DoSomethingConst(&myB);
   //do something else with myA.m_num (2)
}
SomeOtherFunction
内部,编译器不能在(1)期间将
myA.m_num
的值保存在寄存器中,并在(2)期间再次使用它。即使
DoSomethingConst
const
,因此不应更改
myA.m_num
的值,该值仍然可以更改,因为
myB
中有一个指向
myA
的非常量指针,因此
myA.m_num
myA.DoSomethingConst
期间仍然可以更改。在这种情况下,证明存在对
myA
的非常量引用并不重要,但在一般情况下并非如此