mercredi 21 février 2018

User-defined literal operator (constexpr constructor)

I'm implementing a class that defines a complex number with the possible arithmetics that goes with it. Now, I am trying to write a user-defined literal operator:

constexpr Complex operator "" _i(long double arg) {
    return Complex{0.0, arg};
}

This lets me write the following:

Complex c = 1.0 + 5.0_i;

For this, I have to implement a constexpr constructor that goes with it:

constexpr Complex(double real, double imaginary) : re(real), im(imaginary) {};

where re and im are the member variables. This works fine when compiling in g++ but as soon as I'm using clang I get this error:

error: constexpr function never produces a constant expression[-Winvalid-constexpr]

Am I doing something wrong? Is g++ less restrictive than clang?

Aucun commentaire:

Enregistrer un commentaire