I cannot understand the purpose of marking the setter function as constexpr
, that is allowed since C++14.
My misunderstanding comes from the next situation:
I declare a class with a constexpr c-tor, and I am about to use it in a constexpr context, by creating a constexpr instance of that class constexpr Point p1
. An object p1
now is constant and its value could not be changed, so the constexpr
setter could not be called.
On the other hand, when I create an instance of my class Point
in a non-constexpr context Point p
, I can call the setter for that object, but now setter will not execute at compile-time, because the object is not constexpr!
As a result, I do not understand how can I enhance the performance of my code using constexpr
for setters.
This is the code that demonstrates calling a constexpr setter on an non-constexpr object, that means run-time computation, instead of the compile-time:
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);
}
Could anyone help me to understand what is the purpose of marking the setter function as constexpr
?