lang/conceptnttp.cpp

The following code example is taken from the book
C++20 - The Complete Guide by Nicolai M. Josuttis, Leanpub, 2021
The code is licensed under a Creative Commons Attribution 4.0 International License. Creative Commons License

// raw code

#include <bit>

template<auto Val>
concept PowerOf2 = std::has_single_bit(static_cast<unsigned>(Val));

template<typename T, auto Val>
requires PowerOf2<Val>
class Memory {
  //...
};

int main()
{
   Memory<int, 8> m1;     // OK
   Memory<int, 9> m2;     // ERROR
   Memory<int, 32> m3;    // OK
   Memory<int, true> m4;  // OK
   //...
}