Removing const
from line 12 prevents the class What
from being instantiated during compilation. I do not expect What
to ever be instantiated, regardless of constness in the declaration. This is consistent between clang, gcc and MSVC so I assume it is standard. Marking the constructor explicit
also does not prevent the instantiation. What am I not understanding here? Why does constness make a difference?
template <typename T> constexpr bool just_false() { return false; }
template<typename T>
class What {
static_assert(just_false<T>(), "Why was this class instantiated?");
};
struct The {};
struct Heck {
Heck(The) {}
Heck(const What<int>&); // Removing 'const' from this line stops 'What<int>' from being instantiated
};
int main() {
The the;
Heck{the};
}
The just_false
incantation is just to prevent the static assert from always triggering regardless of instantiation.
Compiler explorer link: https://godbolt.org/z/8cETcfss5