vendredi 3 juin 2016

C++ - Check if all template arguments are power of 2

I`am trying to find a simple way of checking if parameters passed as template arguments are all power of 2. I found a bithack on the website and I have this:

constexpr bool isPowerOf2(size_t value){
return !(value == 0) && !(value & (value - 1));
}

This works nice for single value but applying this to multiple arguments looks ugly.

static_assert(isPowerOf2(Arg1), "Argument must be a power of 2");
static_assert(isPowerOf2(Arg2), "Argument must be a power of 2");
static_assert(isPowerOf2(Arg3), "Argument must be a power of 2");

It would be better if I could make it look like arePowersOf2(Arg1, Arg2, Arg3), but im not really advanced in template magic. So my question: Is there an easy way of doing it? I would prefer constexpr C++11 solution.

Aucun commentaire:

Enregistrer un commentaire