I would like to implement a template function which generates bitmasks in compile-time for integral types. These masks should be based on 8-bit patterns where the pattern will be repeated consecutively to fill the integer. The following example does exactly what I want but in run-time:
#include <iostream>
#include <type_traits>
#include <cstring>
template<typename Int>
typename std::enable_if<std::is_integral<Int>::value, Int>::type
make_mask(unsigned char pattern) {
Int output {};
std::memset(&output, pattern, sizeof(Int));
return output;
}
int main() {
auto mask = make_mask<unsigned long>(0xf0);
std::cout << "Bitmask: '" << std::hex << mask << "'" << std::endl;
}
The output of the code above is:
Bitmask: 'f0f0f0f0f0f0f0f0'
I know that the optimizer can eliminate the entire function call in the code above, but I'm looking for a constexpr solution with c++14 and optionally with c++11.
Aucun commentaire:
Enregistrer un commentaire