c++ - GCC 会优化掉内联访问器吗?

标签 c++ c++11 gcc optimization accessor

假设我有这门课

class Point 
{
  inline float x() const { return v[0]; }
  inline float y() const { return v[1]; }
  inline float z() const { return v[2]; }

  float v[3];
};

我这样做:
Point myPoint;
myPoint[0] = 5;

// unrelated code goes here

float myVal = myPoint.x() + 5;

请问GCC上-O2-O3优化掉对 x() 的任何调用刚刚获得 v[0] ? IE:
float myVal = myPoint.v[0] + 5;

或者有什么理由说明这是不可能的?

更新:应该提到我确实意识到 inline对编译器的建议比其他任何东西都重要,但无论如何都想知道。

作为一个附加问题,对此类进行模板化是否会对可以进行的优化产生任何影响?

最佳答案

Will GCC optimize away an inline accessor?



所有优化编译器都会这样做。与其他优化相比,这是一个微不足道的优化。

Or is there a reason why this is impossible?



没有任何理由使它不可能,但也没有保证。

As an additional question, will templating this class have any effect on the optimizations that can be done?



不是。但是,当然,编译器可能有不同的模板内联阈值。

关于c++ - GCC 会优化掉内联访问器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60066017/

相关文章:

c++ - 我可以使用 std::vector 作为预分配(原始)数组的外观吗?

c++ - 使用 std::bind 时出现运行时错误

c - 检测 C 中未使用的函数

c - HDF-EOS 配置失败, "C compiler cannot create executables"

c++ - 丢弃限定符未知原因 (std::bind()/lambda)

c++ - 无法使用 GCC 编译器 4.7.3 在 AIX 上编译具有线程支持的 C++ 程序

c++ - VS2015 的 Constexpr 查找表

c++ - 从文件中删除注释并保留整数

c++ - 使用别名和 C++ 集成在 .qml 中实现 .ui 文件的逻辑

c++ - 是否有一些技巧可以让我将流操纵器传递给可变参数模板函数?