cppreference.com states that static_assert needs to be supplied a constexpr
value of type bool
. And that the method std::optional::operator bool()
is declared constexpr
.
This actually is about std::optional
as function parameter. Imagine the following:
class T {
public:
virtual void f(std::optional<int> i) {...};
};
class U : public T {
public:
virtual void f(std::optional<int> i) override {
static_assert(i, "This is no longer optional");
f(i.value());
}
virtual void f(int i) {...}
};
The parameter that was optional in the base class transformed to a required parameter.
It is perfectly clear to me that this form of problem originates in bad OO-design, but I can't be asked to refactor it just now. I'll do that some point later (I promise :D). But I'd like to insert a quick compiler check.
My compiler tells me that:
[There is a] non-constant condition for static assertion
Why is this considered non-const?