This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/65853] New: Casting operator is missing ambiguity error (and happily compiles)
- From: "ryan.martindale at raytheon dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Wed, 22 Apr 2015 23:58:23 +0000
- Subject: [Bug c++/65853] New: Casting operator is missing ambiguity error (and happily compiles)
- Auto-submitted: auto-generated
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65853
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 {
public:
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 {
public:
DerivedOnce(const std::string &name) : Base(name) {}
};
class DerivedAgain : public DerivedOnce {
public:
DerivedAgain(const std::string &name) : DerivedOnce(name) {}
};
class DerivedElse : public Base {
public:
DerivedElse(const std::string &name) : Base(name) {}
};
class CombinedOnce : public DerivedOnce, public DerivedElse {
public:
CombinedOnce(const std::string &name1, const std::string &name2) :
DerivedOnce(name1), DerivedElse(name2) {}
};
class CombinedAgain : public DerivedAgain, public DerivedElse {
public:
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;
}