dimanche 25 mars 2018

Linker errors with constexpr arrays

I have been writing code that uses constexpr float[n] to hold coefficients of a polynomial fit. The code boils down to the following snippet:

#include <iostream>

template<typename Real, size_t degree> struct Data;

template<typename Real> struct Data<Real, 3> {
    constexpr static Real coefficients[4] = {-0.18374177,
                                             1.87998084,
                                             -0.86969933,
                                             0.09227796};
};

int main() {
    std::cout << Data<float, 3>::coefficients[0] << std::endl;
}

This compiles fine using g++ (version 5.4.0 and 7.1.0). But causes linker errors on icc (version 18.0.2) and clang++ (version 3.8.0 and version 4.0.1). From this question, I understand that using constexpr on arrays is legal C++ but it also mentions the linker error with clang++. If I remove the template, the code compiles on icc but still gives a linker error with clang++.

So my question is whether this is in fact legal C++(11) or not? If this is legal C++, why is it still not supported by the two other compilers? If it isn't, what makes it illegal?

Any input is greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire