Search code examples
c++templateseigen

How to get parameter value from a templated class


Out of convenience I defined 1d, 2d, 3d and 4d vectors using Eigen::Matrix.

namespace MyNamespace {
    template <class scalar_t, int dim>
    using Vec = Eigen::Matrix<scalar_t, dim, 1, Eigen::ColMajor | Eigen::AutoAlign, dim, 1>;

    typedef Vec<double, 1> Vec1d;  // Convenience typedef for 1d vector of doubles.
    typedef Vec<double, 2> Vec2d;  // Convenience typedef for 2d vector of doubles.
    typedef Vec<double, 3> Vec3d;  // Convenience typedef for 3d vector of doubles.
    typedef Vec<double, 4> Vec4d;  // Convenience typedef for 4d vector of doubles.
}

I then defined a templated class, which should be able to store the dimension dim as en enumerable:

namespace MyNamespace {
/**
 * A class representing Monomial basis.
 */
template <typename vec_t>
class Monomials {
  public:
    typedef vec_t vector_t;   ///< Vector type.
    /// Store dimension of the domain.
    enum { /** Dimensionality of the domain. */ dim = vec_t::dim };

  public:
    /**
     * Construct a basis of all monomials
    */
    Monomials(int order){
        printf("order: %d", order);
        printf("dimension: %d", vec_t::dim); // < ------ I want this!
    };
};
}

However, this does not compile (reproduceable here: https://godbolt.org/z/ro7fn6o3f)

using namespace  MyNamespace;    

int main(){
    auto p = Vec2d{0.0, 1.0};

    printf("%f, %f\n", p[0], p[1]);
    auto m = Monomials<Vec2d>(2);

    return 0;
}

Instead, I get an error saying that

<source>:21:62: error: 'dim' is not a member of 'Eigen::Matrix<double, 2, 1>'

How should it work?

Well all I want is basically to get some sort of Vec2d::dim functionality, that would return me the dimension of a given vector, in this case 2.


Solution

  • You can use RowsAtCompileTime

    printf("dimension: %d", vec_t::RowsAtCompileTime);
    

    With this I'm able to get your example to print

    0.000000, 1.000000
    order: 2dimension: 2