例如,我有一个类。
class Point
{
public:
float operator[](int i) const
{
if (i == 0) return m_x; // simple ifs, performance reduction??
if (i == 1) return m_y;
return m_z;
}
private:
float m_x;
float m_y;
float m_z;
};
与访问std::array<float,3>
元素相比,是否存在性能下降? 如果有的话,我该如何消除它。 我想使用字段x、y、z而不是数组。
if
语句的参数是一个常量表达式,那么它在运行时可能是“免费”的。但这里并非如此。 - Chris Beck