tmolteno / template-fft

Fast Header only C++ FFT and Inverse FFT using templates
GNU General Public License v2.0
11 stars 0 forks source link

Adding constraints. #1

Open bob-kerner opened 3 weeks ago

bob-kerner commented 3 weeks ago

I thought I'd contribute a tiny snippet of code to constrain the integer values used for template specialization to powers of two using the new c++20 requires feature.

#include <concepts>

constexpr bool is_power_of_two(std::integral auto value) {
    return (value > 0) && ((value & (value - 1)) == 0);
}

template<std::integral auto i>
requires (is_power_of_two(i))
struct Foo{

};
tmolteno commented 3 weeks ago

Hi. Many thanks for this snippet. That is a neat test for whether an integer is a power of two - haven't seen it before.