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
};
I 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?
1