Bug 77660 - Conversion operator unknown or ambiguous: gcc or clang behave differently
Summary: Conversion operator unknown or ambiguous: gcc or clang behave differently
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 5.4.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid, rejects-valid
Depends on:
Blocks:
 
Reported: 2016-09-20 11:34 UTC by Naupacte
Modified: 2022-11-20 19:31 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Naupacte 2016-09-20 11:34:11 UTC
In relation with bug #50306:
(using gcc5.4.1 or gcc6.1 from macports)

struct A;
struct B;

template <class T> struct Ptr
{
	template <class U> operator const Ptr<U>& () const;
#ifdef GCC_BUT_CLANG_DECLARES_IT_IS_AMBIGUOUS
	template <class U> operator       Ptr<U>  () const; // required by gcc but skipped for clang
#endif
};


const Ptr<A>& foo1 (const Ptr<B>& b) { return b;}//gcc ok 
      Ptr<A>  foo2 (const Ptr<B>& b) { return b;}// needs GCC_BUT_CLANG_DECLARES_IT_IS_AMBIGUOUS to be set
      Ptr<A>  foo2a(const Ptr<B>& b) { return static_cast<Ptr<A>>(b);} //gcc patch


Who is right? Gcc cannot convert from const Ptr<B>& to Ptr<A> via const Ptr<A>&. I guess it has an influence on its inability to get the common type for brace-lists or ?: pairs when only the reference conversion is given.

To avoid error: define GCC_BUT_CLANG_DECLARES_IT_IS_AMBIGUOUS only for gcc. Otherwise, we get:

GCC
====
/opt/local/bin/gcc-mp-6 -x c++ -std=c++1y -c program.cpp

program.cpp: In function 'Ptr<A> foo2(const Ptr<B>&)':
program.cpp:22:47: error: could not convert 'b' from 'const Ptr<B>' to 'Ptr<A>'
       Ptr<A>  foo2 (const Ptr<B>& b) { return b;}// needs GCC_BUT_CLANG_DECLARES_IT_IS_AMBIGUOUS to be set

CLANG
=====

clang-mp-3.8 -x c++ -std=c++1y -c program.cpp -DGCC_BUT_CLANG_DECLARES_IT_IS_AMBIGUOUS

program.cpp:22:47: error: conversion from 'const Ptr<B>' to 'Ptr<A>' is ambiguous
      Ptr<A>  foo2 (const Ptr<B>& b) { return b;}// needs GCC_BUT_CLANG_DECLARES_IT_IS_AMBIGUOUS to be set
                                              ^
program.cpp:13:76: note: candidate function [with U = A]
        template <class U> operator const Ptr<U>& () const;
                           ^
program.cpp:15:76: note: candidate function [with U = A]
        template <class U> operator       Ptr<U>  () const;
                           ^
program.cpp:23:47: error: ambiguous conversion for static_cast from 'const Ptr<B>' to 'Ptr<A>'
      Ptr<A>  foo2a(const Ptr<B>& b) { return static_cast<Ptr<A>>(b);} //gcc patch
                                              ^~~~~~~~~~~~~~~~~~~~~~
program.cpp:11:27: note: candidate is the implicit copy constructor
template <class T> struct Ptr
                          ^
program.cpp:11:27: note: candidate is the implicit move constructor
2 errors generated.
Comment 1 Andrew Pinski 2022-11-20 19:31:48 UTC
I am pretty sure this is really a dup of bug 61663.