How I can compile template function with pre-processor condition? Like that (but it is not working):
template
void f()
{
#if (var == tru
With C++17's introduction of if constexpr you can discard branches inside a template, much like conditional compilation allows.
template
void f()
{
if constexpr (var == true) {
// ...
}
}
The code inside the branch has to be syntactically correct, but doesn't have to be well-formed when var
is false, because it will be discarded entirely.