This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
g++ does not use conversion function for implicit conversion to another type
- From: André Ritter <andre dot ritter at fau dot de>
- To: gcc-help at gcc dot gnu dot org
- Date: Wed, 30 Oct 2013 10:50:30 +0100
- Subject: g++ does not use conversion function for implicit conversion to another type
- Authentication-results: sourceware.org; auth=none
Hi,
in a c++ software project I want to use a construct which is similar to the following c++ test case:
> struct A
> {
> virtual void do_it() const = 0;
> };
>
> struct B : A
> {
> virtual void do_it() const {}
> };
>
> struct C
> {
> operator B() const { return B(); }
> };
>
> void do_it(const A& a) { a.do_it(); }
>
> int main()
> {
> C c;
> do_it(c);
> return 0;
> }
When compiling this test case with clang++ (version 3.3) and the intel c++ compiler (versions 12.1 and 13.1), compilation succeeds without errors. When compiling the same test case with g++ (version 4.8.1) the following errors are shown:
> cast.cc: In function 'int main()':
> cast.cc:21:9: error: cannot allocate an object of abstract type 'A'
> do_it(c);
> ^
> cast.cc:1:8: note: because the following virtual functions are pure within 'A':
> struct A
> ^
> cast.cc:3:15: note: virtual void A::do_it() const
> virtual void do_it() const = 0;
> ^
Basically I have an abstract base class A and some implementation B. Another class C provides a conversion operator to B. When passing instances of C to functions that take const references of A clang++ and icpc is able to find the conversion to B and is then able to convert the temporary instance of B to a const reference of A. I think, in the same case, g++ tries to make a temporary instance of A initialized with the instance of C which is not possible.
A workaround for this problem is to explicitly convert the instance of C to B like this:
> do_it((B)c);
But then it would be nice if this is not necessary.
Is the behavior of clang and icpc not conforming to the c++ standards or is this kind of a bug of g++?
Regards,
André Ritter