Probably easiest to explain if I show an example:
#include <iostream>
#include <type_traits>
class Unconstructible
{
public:
Unconstructible() = delete;
};
int main()
{
if constexpr (std::is_constructible_v<Unconstructible>)
{
std::cout << "Unconstructible is constructible. What?\n";
// Unconstructible foo;
}
else
{
std::cout << "Unconstructible is not constructible.\n";
}
return 0;
}
This outputs "Unconstructible is not constructible" as expected. If, however, I uncomment the line Unconstructible foo;
, I get a compilation error:
1>F:\Programming\C++\CppScratchwork\CppScratchwork\CppScratchwork\CppScratchwork.cpp(48): error C2280: 'Unconstructible::Unconstructible(void)': attempting to reference a deleted function
1>F:\Programming\C++\CppScratchwork\CppScratchwork\CppScratchwork\CppScratchwork.cpp(39): message : see declaration of 'Unconstructible::Unconstructible'
1>F:\Programming\C++\CppScratchwork\CppScratchwork\CppScratchwork\CppScratchwork.cpp(39,5): message : 'Unconstructible::Unconstructible(void)': function was explicitly deleted
1>Done building project "CppScratchwork.vcxproj" -- FAILED.
That's in MSVC using /std:c++17. The same behavior shows up on GCC with -std=c++17 but with differently-worded error messages involving attempting to reference a deleted function.
Why is it even attempting to compile the first part of the if constexpr
branch? As far as I can tell, it is correctly able to tell that std::is_constructible_v<Unconstructible>
is false
.