dimanche 19 juillet 2020

Template Parameter Array

I'm trying to create a class that uses the value of an array as a value/type template parameter. See below

template< std::array<bool, M> arr>
class gf {
...
};

I believe my rationale for doing this is sane. This class implements operator overloading for addition and multiplication. These operations are only well defined if the instances of the class were instantiated with the same value and size of arr.

An example usecase:

std::array<bool,3> p1 = {0,0,1};
std::array<bool,3> p2 = {0,1,1};

gf<p1> a;
gf<p1> b;
gf<p2> c;

auto out1 = a + b; // works as both instances use p1
auto out1 = a + c; // Compiler error as we're adding incompatible types

My current work around is passing arr to the constructor call and throwing an error if any incompatible types are combined. I hope there's a better way to do this, thanks!

Aucun commentaire:

Enregistrer un commentaire