Tuesday, October 17, 2017

C++17: A compile-time static if

C++17 added a compile-time static if. It’s purpose is to allow the compiler to eliminate branches of an if statement. Here is an example:

#include <iostream>

int main()
{
  if constexpr (0)
  {
    std::cout << "Section Compiled Out" << " ";
  }
  else
  {
    std::cout << "Section Compiled In" << " ";
  }

  std::cout << std::endl;
  return 0;
}
// Output: Section Compiled In
References:
https://en.wikipedia.org/wiki/C%2B%2B17
https://tech.io/playgrounds/2205/7-features-of-c17-that-will-simplify-your-code/constexpr-if

No comments:

Post a Comment