In the following code sample, the if
statement depends on bool
template parameter, which is a compile-time constant. Compilers handle this code differently:
MSVC fails with link error (which is what I expected), because the template function in
else
branch lacks specialisation fortrue
template parameter value (even though it is never called).GCC and Clang both compile without issues and run-time behaviour is correct. This is obviously because they evaluate the
if
statement at compile time and remove unused branches before linking.
The question is which behaviour is standard-compliant (or is it an undefined behaviour and both are correct in their own way)?
#include <iostream>
template<const bool condition>
struct Struct
{
void print()
{
if (condition)
{
std::cout << "True\n";
}
else
{
printIfFalse();
}
}
private:
void printIfFalse();
};
template <>
void Struct<false>::printIfFalse()
{
std::cout << "False\n";
}
int main()
{
Struct<true> withTrue{};
withTrue.print();
Struct<false> withFalse{};
withFalse.print();
return 0;
}