c++templatesvariadic-templates

Is it possible to reference template template arguments?


I am experimenting with templates in C++ by implementing linear algebra library, thus this example might not be the best fit for the job.

Let's say that I have an N-dimensional container which contains data and common functions of vectors and matrices from which I want to inherit (I am aware, that it is a common practice to define Matrix type and then use using declaration for different linear algebra types like they do in eigen, but I want to inherit this container and then add methods to various types. Again, it might not be the best fit, but it is an experiment).

template <typename T, std::size_t... Dims>
class NDimContainer {
public:
    auto operator+=(const NDimContainer &other) {
        std::transform(elems.begin(), elems.end(), other.begin(), std::plus<T>());
        return *this;
    }
private:
    std::array<T, (... * Dims)> elems;
};

template <typename T, std::size_t Dims>
class Vector : public NDimContainer<T, Dims> { ... }

The question is: is it possible to access template arguments N and Dims... specified in the template template like this:

template <template<typename N, std::size_t... Dims> typename T>
require std::is_derived_from<NDimContainer<N, Dims...>, T<N, Dims...>>
auto operator+(const T &lhs, const T& rhs) { 
    return T { lhs } += rhs;
}

I know that it is possible do this:

template <template<typename, std::size_t...> typename T, typename N, std::size_t... Dims>
require std::is_derived_from<NDimContainer<N, Dims...>, T<N, Dims...>>
auto operator+(const T &lhs, const T& rhs) { ... }

But then the template won't get matched for e.g. Vector type (or at least in my case it didn't, if it is some trivial mistake, please correct me).

Is there any way to make this work so that I can do something like this?

Vector<int, 3> a = Vector<int, 3>() + Vector<int, 3>();

I have found previous questions about this topic here and here. But none of these really solves my use case.

The culprit that prohibits much simpler solution is Dims..., because I don't know how to do something like this:

template <typename T, std::size_t... Dims>
class A {
public:
    using Type = T;
    using Dimensions = Dims...; // Error
};

It probably could be possible to use std::integer_sequence, but that would introduce additional space costs which I don't want pay.

I have tried something like this:

template <template<typename N, std::size_t... Dims> typename T, typename NN = N, std::size_t... DDims = Dims>
require std::is_derived_from<NDimContainer<N, Dims...>, T<N, Dims...>>
auto operator+(const T &lhs, const T& rhs) { ... }

But that obviously also didn't work.

Is some similar construction possible?

edit: Adding minimal reproducible example

#include <array>
#include <concepts>

template<typename T, std::size_t ... Dims>
struct A {
    std::array<T,(Dims*...)> data = {};
    A& operator+=( const A& ) noexcept;
};

// This is the one I would like it matched instead, because then it would be instantiated for
// particular type.
template<template<typename N, std::size_t... Dims> typename T>
requires std::derived_from<A<N, Dims...>, T<N, Dims...>>
auto operator+(const A<N, Dims...>& lhs, const A<N, Dims...>& rhs) -> A<N, Dims...> {
    return A{lhs} += rhs;
}

template<typename T, std::size_t N>
struct Vector : A<T,N> {
    using Base = A<T,N>;

    Vector() = default;
    Vector(const Base& base) : Base(base) {}
};

using Vector3d = Vector<double,3>;

int main() {
    Vector3d vec = Vector3d{} + Vector3d{};

    return 0;
}

Solution

  • Your question is too long and there are too many questions in it. I will focus on this:

    The question is: is it possible to access template arguments N and Dims... specified in the template template like this:

    template <template<typename N, std::size_t... Dims> typename T>
    require std::is_derived_from<NDimContainer<N, Dims...>, T<N, Dims...>>
    auto operator+(const T &lhs, const T& rhs) { 
        return T { lhs } += rhs;
    }
    

    No. Here template <template<typename N, std::size_t... Dims> typename T> you are saying that T is a template with parameters N, and Dims.... You can instantiate T, but you cannot deduce from T the values of N and Dims... because they are parameters.

    What you are asking is similar to: "Given a function f that takes a int x as parameter, can I find out what the value of x is?". Of course not. f is a function to which any x can be passed.

    I sense a pointless use of template template argument like I have seen it before. I suppose you actually want a type and something like this:

     template <typename T>
     require std::is_derived_from<NDimContainer<T>,T>
     auto operator+(const T &lhs, const T& rhs) { 
         return T { lhs } += rhs;
     }
    

    Where T is an instantiation of some template that had parameters N and Dims... If that is the case, then you can deduce from that instantiation T (a type), the parameters N and Dim..., such that T is the same type as some U<N,Dims...>. That is possible, but a different question.