The following code compiles on a wide range of gcc and clang versions - when compiled and run with gcc 5.3.1, it prints
A()
then aborts with a pure virtual call error.
#include <stdio.h>
class A
{
public:
A() {
printf("A()\n");
}
virtual void b() const = 0;
};
int main()
{
const A& a{};
a.b();
return 0;
}
I realise binding a reference to a temporary is not ideal (though I think this case is covered by some sort of lifetime extension) - but it also works when trying to call a method that takes a const reference like:
Foo({});
For convenience, assuming links from new users are not banned, here's an example of it compiling with clang 3.2: Compiler Explorer
Aucun commentaire:
Enregistrer un commentaire