This question already has an answer here:
- Why do gcc and clang allow me to construct an abstract class? 1 answer
I would like to understand why the compiler allows the following code to compile
#include <iostream>
struct A
{
A()
{
std::cout << "A::A\n";
}
virtual void f() const = 0;
};
void g(const A& a)
{
a.f();
}
int main()
{
g({});
}
It even outputs A::A
when run.
If I replace g({})
with g(A())
it obviously doesn't compile. It complains that A
is abstract and cannot be instantiated. Both Clang and GCC compile this fine without any warnings. When run both versions print pure virtual method called
and terminate.