mercredi 14 septembre 2016

Do all constructors need to be constexpr if any are to be constexpr?

With the following example code:

template<typename T, size_t N> class V
{
public:
    // constexpr constructor
    constexpr V(const std::array<T, N>& v) : Elts_(v) {}

    // non-constexpr constructor
    V(T elt = 0) { Elts_.fill(elt); }

private:
    std::array<T, N> Elts_;
}

I am not able to instantiate a constexpr object:

constexpr auto Z = V<double, 3>({0., 0., 1.});

However, with a class derived from this base that has all constructors declared constexpr, I can instantiate constexpr objects.

Must one declare all constructors constexpr to be allowed to instantiate constexpr objects? Or is there something else going wrong here? I don't see anything in the standards prohibiting mixing constexpr and nonconstexpr constructors in a class.

This question is particular to c++11; though information on how this may be different in c++14 and c++17 is certainly welcome.

Aucun commentaire:

Enregistrer un commentaire