Passing an abstract object by weirdness value in clang ++

why is clang ++ 3.6 compiling the following code (g ++ not)?

class Abc { public: virtual void foo() const = 0; virtual ~Abc() {} }; // is correctly rejected // void bar(Abc o) // { // } class B { void bar(Abc o) // should also be rejected { } }; int main() { } 

I am using clang 3.6 and gcc 4.9.2.

Why is a free function (correctly) rejected and the member function is not?

Any clues? Error in clang?

If I modify above:

 class Abc { public: virtual void foo() const = 0; virtual ~Abc() {} }; class Impl : public Abc { public: void foo() const {} }; class B { public: void bar(Abc o) { o.foo(); } }; int main() { B b; Impl i; b.bar(i); } 

I get

main.cc:16: undefined reference to `Abc :: foo () const '

Linker Error.

So the question is: why does clang ++ compile this incorrect code at all? I would say that this is a serious mistake!

+6
source share
1 answer

As N4296 10.4 [class.abstract] says:

An abstract class should not be used as a parameter type, as a function of a return type, or as an explicit conversion type. Pointers and can declare references to an abstract class.

 [ Example: shape x;// error: object of abstract class shape* p;// OK shape f();// error void g(shape);// error shape& h(shape&);//OK — end example ] 

So gcc follow the standard.

Why can't an abstract type parameter be declared? Suppose that when an object of the subclass pass in bar, the objects are split, oh, an object that has a pure virtual function ... This is a contradiction. Perhaps the reason.

EDIT:

Why clang passes it is a clang compiler problem.

+1
source

Source: https://habr.com/ru/post/984305/


All Articles