Are modern C++ compilers able to avoid calling a const function twice under some conditions?

后端 未结 5 867
被撕碎了的回忆
被撕碎了的回忆 2021-02-05 01:10

For instance, if I have this code:

class SomeDataProcessor
{
public:
    bool calc(const SomeData & d1, const SomeData & d2) const;
private:
    //Some n         


        
5条回答
  •  后悔当初
    2021-02-05 01:56

    No, the compiler is not allowed to do that in this case. The const only means you do not change the state of the object the method belongs to. However, invoking this method multiple times with the same input parameters might give different results. For example, think of a method that produces a random result.

提交回复
热议问题