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

后端 未结 5 870
被撕碎了的回忆
被撕碎了的回忆 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:42

    GCC has the pure attribute (used as __attribute__((pure))) for functions which tells the compiler that redundant calls can be eliminated. It's used e.g. on strlen.

    I'm not aware of any compiler doing this automatically, especially considering the fact that the functions to be called may not be available in source form, and the object file formats contain no metadata about whether a function is pure or not.

提交回复
热议问题