For the past hour I've been trying to figure out why a bunch of static_asserts were going off in my library after I updated to the latest version of Visual Studio 2022, and so I made a little test to see if static_assert was behaving differently, and it certainly is:
#include <concepts>
template <typename type_p>
constexpr bool
True()
noexcept
{
static_assert(false, "I used to never assert because my function is never instantiated!");
return true;
}
template <std::same_as<int> type_p>
constexpr int
Error_Code()
noexcept
{
if constexpr (!std::same_as<type_p, int>) {
static_assert(false, "I used to never assert because my branch is never instantiated!");
}
return 0;
}
int main(void)
{
return Error_Code<int>();
}
I was dumbfounded when I tested this code on all three major compilers and found that they all tripped on both asserts, so I figure this must be standards compliant somehow. Is this something to do with "no diagnostic required" and now msvc is doing the diagnostic?
Also, can you give an example of how to achieve the same thing as a static_assert(false) in a constexpr if/else branch that should never be instantiated, because that's incredibly useful. C++20 answers are welcome.
Thanks!