Here is my code:
class test {
public:
constexpr test() {
}
constexpr int operator+(const test& rhs) {
return 1;
}
};
int main() {
test t; //constexpr keyword isn't necessary
constexpr int b = t+test(); // works at compile time!
int w = 10; // ERROR constexpr required
constexpr int c = w + 2; // Requires w to be constexpr
return 0;
}
I notice that it worked even though I didn't specify test to be constexpr
. I tried replicating the result by doing the same with int
but I get errors. Specifically, it wants my int w
inside the constexpr int c = w + 2;
to be constexpr
. In my first attempt which is using test
, did it work because I used constexpr
on the constructor already? If that is the case, then, is it correct to assume that all classes that have constexpr
constructors will result in all objects that are instantiated or created with these constructors to be constexpr
?
Bonus question:
If I have a constexpr
constructor, is it bad to do something like test* t = new test();
?