Home > Back-end >  Constexpr methods for nonconstexpr class
Constexpr methods for nonconstexpr class

Time:07-04

Is there any reason to add constexpr to class's methods if class hasn't any constexpr constructor? Maybe compiler can do some optimizations in this case?

CodePudding user response:

Yes, one obvious case is when the class is an aggregate class. Aggregate initialization doesn't call any constructor, but can still be used in constant expression evaluation.

Even if the class is not an aggregate class, you can still call a constexpr member function in constant expression evaluation if the member function doesn't access any state of the class instance. This is obviously the case for static member functions, but can also apply to non-static member functions.

For example, the following is valid:

struct A {
    int i;        

    // The only usable constructor is the default constructor,
    // which is not `constexpr`.
    // The class is also not an aggregate class because of these declarations.
    A() : i(0) {}
    A(const A&) = delete;

    constexpr int getZero() {
        // return i; would be IFNDR
        return 0;
    }
};

int main() {
    A a;
    constexpr int x = a.getZero();
}

You must be careful though, because if it is completely impossible to call the member function as subexpression of any constant expression, then marking it constexpr anyway makes the program ill-formed, no diagnostic required (IFNDR). In other words the compiler may refuse to compile such a program.

Also, how often such a situation comes up and the constant evaluation use case is intended (especially for non-static member functions), is a different question.

  • Related