将set函数(setter)标记为constexpr的目的是什么?
What is the purpose of marking the set function (setter) as constexpr?
我无法理解将 setter 函数标记为 constexpr
的目的,这是自 C++14 以来允许的。
我的误解来自下一种情况:
我声明了一个带有 constexpr c-tor 的 class,我将通过创建 class constexpr Point p1
的 constexpr 实例在 constexpr 上下文中使用它。对象 p1
现在是常量,它的值无法更改,因此无法调用 constexpr
setter。
另一方面,当我在非 constexpr 上下文 Point p
中创建 class Point
的实例时,我可以为该对象调用 setter,但现在 setter 将在编译时不执行,因为对象不是 constexpr!
因此,我不明白如何在 setter 秒内使用 constexpr
提高我的代码的性能。
这是演示在非 constexpr 对象上调用 constexpr setter 的代码,这意味着 运行 时间计算,而不是编译时间:
class Point {
public:
constexpr Point(int a, int b)
: x(a), y(b) {}
constexpr int getX() const noexcept { return x; }
constexpr int getY() const noexcept { return y; }
constexpr void setX(int newX) noexcept { x = newX; }
constexpr void setY(int newY) noexcept { y = newY; }
private:
int x;
int y;
};
int main() {
Point p{4, 2};
constexpr Point p1{4, 2};
p.setX(2);
}
谁能帮我理解将 setter 函数标记为 constexpr
的目的是什么?
基本上,当你必须处理 constexpr 函数时,这很好。
struct Object {
constexpr void set(int n);
int m_n = 0;
};
constexpr Object function() {
Object a;
a.set(5);
return a;
}
constexpr Object a = function();
想法是能够在将在编译时执行的另一个函数中执行编译时初始化。未完成应用于 constexpr
对象。
另一件需要知道的事情是 constexpr
成员函数不是 const
自 C++14 以来的成员函数 :).
带有 constexpr
限定符的函数将在编译时 评估函数 return ,这可以显着提高程序的性能(没有额外的计算,无指令计数器跳转等)。因此,限定函数有一些要求,因此请查看 IBM 中的解释。
C++14 的新 constexpr 规则出现了这种需求:在 constexpr 函数内部,您现在可以使用多个语句,包括 for 循环和控制流。
这是一个例子:
constexpr int count5(int start) {
int acc = 0;
for (int i = start ; i<start+5 ; ++i) {
acc += i;
}
return acc;
}
constexpr int value = count5(10); // value is 60!
如您所见,我们可以在 constexpr 上下文中对变量进行许多修改。编译器变得像一个解释器,只要 constexpr 函数的结果是一致的并且你不改变已经计算的 constexpr 变量,它可能会在解释过程中改变值。
我无法理解将 setter 函数标记为 constexpr
的目的,这是自 C++14 以来允许的。
我的误解来自下一种情况:
我声明了一个带有 constexpr c-tor 的 class,我将通过创建 class constexpr Point p1
的 constexpr 实例在 constexpr 上下文中使用它。对象 p1
现在是常量,它的值无法更改,因此无法调用 constexpr
setter。
另一方面,当我在非 constexpr 上下文 Point p
中创建 class Point
的实例时,我可以为该对象调用 setter,但现在 setter 将在编译时不执行,因为对象不是 constexpr!
因此,我不明白如何在 setter 秒内使用 constexpr
提高我的代码的性能。
这是演示在非 constexpr 对象上调用 constexpr setter 的代码,这意味着 运行 时间计算,而不是编译时间:
class Point {
public:
constexpr Point(int a, int b)
: x(a), y(b) {}
constexpr int getX() const noexcept { return x; }
constexpr int getY() const noexcept { return y; }
constexpr void setX(int newX) noexcept { x = newX; }
constexpr void setY(int newY) noexcept { y = newY; }
private:
int x;
int y;
};
int main() {
Point p{4, 2};
constexpr Point p1{4, 2};
p.setX(2);
}
谁能帮我理解将 setter 函数标记为 constexpr
的目的是什么?
基本上,当你必须处理 constexpr 函数时,这很好。
struct Object {
constexpr void set(int n);
int m_n = 0;
};
constexpr Object function() {
Object a;
a.set(5);
return a;
}
constexpr Object a = function();
想法是能够在将在编译时执行的另一个函数中执行编译时初始化。未完成应用于 constexpr
对象。
另一件需要知道的事情是 constexpr
成员函数不是 const
自 C++14 以来的成员函数 :).
带有 constexpr
限定符的函数将在编译时 评估函数 return ,这可以显着提高程序的性能(没有额外的计算,无指令计数器跳转等)。因此,限定函数有一些要求,因此请查看 IBM 中的解释。
C++14 的新 constexpr 规则出现了这种需求:在 constexpr 函数内部,您现在可以使用多个语句,包括 for 循环和控制流。
这是一个例子:
constexpr int count5(int start) {
int acc = 0;
for (int i = start ; i<start+5 ; ++i) {
acc += i;
}
return acc;
}
constexpr int value = count5(10); // value is 60!
如您所见,我们可以在 constexpr 上下文中对变量进行许多修改。编译器变得像一个解释器,只要 constexpr 函数的结果是一致的并且你不改变已经计算的 constexpr 变量,它可能会在解释过程中改变值。