Why does the following code not compile? The static assertion checks that Foo is not copy assignable, so I would expect that the compiler discards the code in the constexpr-if block, but it doesn't.
#include <type_traits>
struct Foo {
Foo() = default;
Foo const& operator=(Foo const&) = delete;
};
int main()
{
static_assert(!std::is_copy_assignable_v<Foo>);
Foo x;
Foo other;
if constexpr (std::is_copy_assignable_v<Foo>) {
other = x;
}
return 0;
}
Compiler error:
<source>: In function 'int main()':
<source>:16:17: error: use of deleted function 'const Foo& Foo::operator=(const Foo&)'
16 | other = x;
| ^
<source>:5:16: note: declared here
5 | Foo const& operator=(Foo const&) = delete;
| ^~~~~~~~
Compiler returned: 1
This code snippet
is pesent in a non-template function. So the compiler checks the validaty of the code.
On the other hand, if you have a template code then the code that represents a substatement of the if constexpr statement will not be instantiated if the value of the expression of the if statement is evaluated to false.
Here is a demonstration program.
The program output is
From the C++20 Standard (8.5.1 The if statement):