mardi 15 décembre 2020

Is there a way to force a template argument within a certain range?

Forgive me, as I've tried searching and maybe I'm just using the wrong keywords. I'm almost positive there is a way where I can bound an integer template argument to a positive value. I just know from bitter experience that it's probably not intuitive at all.

Just so you know, STL widgets for doing this (if there are such animals) are pretty much off the table because as much as I love the STL I can't use the STL for the code I'm targeting outside minor stuff like definitions because a lot of the platforms I'm targeting with this code don't have it available. (32kb machines and such)

basically what I want is this

template<const size_t TCapacity /* bounded to a minimum of one */ > class Buffer {
...
};

Where a compile error is thrown if TCapacity is zero such that

Buffer<0> buffer; // won't compile

I mean, not only do i think i have encountered this sort of sorcery before, I've seen things like the Spirit framework do much more complicated things so I'm pretty sure there's an answer. I just doubt it's obvious.

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire