[Bug c++/65853] New: Casting operator is missing ambiguity error (and happily compiles)

ryan.martindale at raytheon dot com gcc-bugzilla@gcc.gnu.org
Wed Apr 22 23:58:00 GMT 2015


            Bug ID: 65853
           Summary: Casting operator is missing ambiguity error (and
                    happily compiles)
           Product: gcc
           Version: 4.8.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ryan.martindale at raytheon dot com

Created attachment 35387
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=35387&action=edit
Source file and artifacts

In the following code, there are three lines commented out because they cause
the compiler to correctly produce an error because of the ambiguity of the
getName function. Unfortunately, I would also expect the other two casting
operator calls to generate a similar ambiguity. Instead g++ compiles and runs
the program just fine, without specifying how it chose the casting operator or
even giving an error.

The lines

  std::string result1(combo1);
  std::string result2(combo2);

should produce an ambiguity error (or at least a warning) rather than compile
and run.

(Note: There are multiple derivations because I was trying to figure out how
the call was actually chosen - it looks to have chosen the first one in the
derived list).

#include <iostream>
#include <string>

class Base {
  Base(const std::string &name) : m_name(name) {}

  operator const std::string &() const { return m_name; }

  const std::string &getName() const { return m_name; }

  const std::string m_name;

class DerivedOnce : public Base {
  DerivedOnce(const std::string &name) : Base(name) {}

class DerivedAgain : public DerivedOnce {
  DerivedAgain(const std::string &name) : DerivedOnce(name) {}

class DerivedElse : public Base {
  DerivedElse(const std::string &name) : Base(name) {}

class CombinedOnce : public DerivedOnce, public DerivedElse {
  CombinedOnce(const std::string &name1, const std::string &name2) :
DerivedOnce(name1), DerivedElse(name2) {}

class CombinedAgain : public DerivedAgain, public DerivedElse {
  CombinedAgain(const std::string &name1, const std::string &name2) :
DerivedAgain(name1), DerivedElse(name2) {}

int main(int argc, const char *argv[]) {

  CombinedOnce combo1("apple", "grape");
  CombinedAgain combo2("apple", "grape");

  std::string result1(combo1);
  std::string result2(combo2);

  std::cout << result1 << " - " << result2 << std::endl;

//  std::string result3(combo1.getName());
//  std::string result4(combo2.getName());

//  std::cout << result3 << " - " << result4 << std::endl;

  return 0;

More information about the Gcc-bugs mailing list