vendredi 29 mars 2019

How to make a type alias depending on a compile-time constant?

I want to write a class that makes use of numerical quadrature. The quadrature order defines the size of some containers that I will use. I would like to make a type alias for such containers and it has to depend on the quadrature order.

The following code shows my trials. It feels suboptimal in the sense that I have to repeat the order in the type alias definition:

#include <array>

class Quadrature
{
public:

        static constexpr unsigned int getOrder()
        {
                return 3;
        }

        // This line doesn't compile!
        //
        // using WeightsContainer = std::array<double, getOrder()>;
        //
        // g++ says "error: 'static constexpr unsigned int Quadrature::getOrder()'
        // called in a constant expression before its definition is complete"

        // This line compiles, but repeats the order. :-(
        using WeightsContainer = std::array<double, 3>;

private:

        WeightsContainer container;
};

One solution that I have found is introducing a template parameter Order. But actually I wanted to determine the quadrature order and introducing the template parameter would make it variable.

Is there a possibility to make the order a compile-time constant and use it within my type alias definition?

Aucun commentaire:

Enregistrer un commentaire