Using static_assert to trigger errors at compile time

If we do the same to the template version, we can utilize static_assert(). The static_assert() declaration, unlike a regular assert, will refuse to compile if the condition isn't fulfilled. So, it's better to break the build than to break at runtime. In the following example, if the template parameter N is a negative number, static_assert() will prevent the function from compiling:

template <typename T, int N> 
auto const_pow_n(const T& v) { 
  static_assert(N >= 0, "N must be positive"); 
  auto product = T{1}; 
  for(int i = 0; i < N; ++i) { 
    product *= v; 
  } 
  return product; 
} auto x = const_pow_n<5>(2); // Compiles, N is positiveauto y = const_pow_n<-1>(2); // Does not compile, N is negative

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.