I have an old C++ project which I recently tried use after not compiling it for a while, only to find that g++ borked on it. The problem is reducible to this: - class A
-
{
-
public:
-
A() { }
-
template <typename T> A(T t) { }
-
};
-
-
template <typename T>
-
A
-
operator-(const A& lhs, T rhs)
-
{ return A(); }
-
-
class B
-
{
-
public:
-
B() { }
-
-
B operator-(unsigned int rhs) { return B(); }
-
};
-
-
int
-
main()
-
{
-
B b;
-
b - 1;
-
return 0;
-
}
-
g++ now complains that "b - 1" is ambiguous, because the C++ standard says so even though the worst conversion for B::operator-(unsigned int) is better than the worst conversion for operator-(const A&, T). Is this true?
4 6219 Banfa 9,065
Expert Mod 8TB
It's true and it is also what you would want. The compiler is acting unintelligently, it is saying neither operation is an exact match therefore I will not compile this, you will have to explicitly state which conversion you want to happen.
There are a number of ways to do this, in your simple example change 1 to 1U, this means no conversion is required for the B::operator- so it is an exact match and is used.
Or you could explicitly invoke the operator by changing b - 1 to b.operator-(1); However the point is that the compiler does secretly make any decisions for you when there is an ambiguity (which is personally how I like all my software not just compilers).
Your class A needs to be a template. -
template<class T>
-
class A
-
{
-
public:
-
A() { }
-
template <typename T> A(T t) { }
-
-
};
-
template<class T>
-
A<T> operator-(const A<T>& lhs, T rhs)
-
{ return A(); }
-
As initially written the compiler can't tell whether to use the inline function of class B: -
B operator-(unsigned int rhs) { return B(); }
-
or to use -
template <typename T>
-
A
-
operator-(const A& lhs, T rhs)
-
{ return A(); }
-
where T is a object of class B or whether to use
By making class A a template, the ambiguity is resolved since -
B operator-(unsigned int rhs) { return B(); }
-
is a better conversion than -
template<class T>
-
A<T> operator-(const A<T>& lhs, T rhs)
-
{ return A(); }
-
where T is an object class B.
@weaknessforcats
But in this case, all A's need to have the same type, no matter what type T was used in its template constructor, or what possibly different type T is subtracted from it. @weaknessforcats
Perhaps you meant: -
template<class T, class U>
-
A<T> operator-(const A<T>& lhs, U rhs)
-
{ return A<T>(); }
-
Either way, I can't really do that. But why is it that the original code is ambiguous? I always thought that overloads were ranked by the worst conversion necessary, and since the worst conversion for B::operator-(unsigned int) (int -> unsigned int) is better than the worst conversion for operator-(const A&, T), T = int (B -> A via user-defined constructor), where is the ambiguity?
When your class A is not a template, there is still a function template for the A constructor. Therefore, when you b-1 the compiler could call: -
template <typename T>
-
A
-
operator-(const A& lhs, T rhs)
-
{ return A(); }
-
The lhs argument could obtained as an A object intialized by a B object using the template A constructor.
Or the compiler could call: -
class B
-
{
-
public:
-
B() { }
-
-
B operator-(unsigned int rhs) { return B(); }
-
};
-
Hence the ambiguity.
When class A is a template then class B is preferred to A<T>.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Alex Zhitlenok |
last post by:
Hi,
My question is how to resolve in C# ambiguous overloaded operators?
Let say, I have two unrelated classes A and B, each one implements
overloaded operator + with the first parameter of type...
|
by: glen |
last post by:
Hi. I'm using GCC 4.1.1, which I mention since I don't know
if this is a compiler issue, or me not understanding some
subtlety in the standard.
The code below compiles fine under vc++, but I'm...
|
by: y-man |
last post by:
Hi,
I am trying to get an overloaded operator to work inside the class it
works on. The situation is something like this:
main.cc:
#include "object.hh"
#include "somefile.hh"
object obj,...
|
by: neildferguson |
last post by:
I am using templates with a little project I am working on. My compiler (GCC)
is finding a particular construct ambiguous. Can anyone suggest something I
might change in the declaration of class...
|
by: onkar |
last post by:
#include<iostream>
using namespace std;
class Integer{
int i;
public:
Integer(int ii):i(ii){}
const Integer operator+(const Integer& rv){
cout<<"1-operator+"<<endl;
return Integer(i+rv.i);
}
|
by: i3x171um |
last post by:
To start off, I'm using GCC4. Specifically, the MingW (setjmp/longjmp) build of GCC 4.2.1 on Windows XP x64.
I'm writing a class that abstracts a message, which can be either an integer (stored as...
|
by: sebastian |
last post by:
I've simplified the situation quite a bit, but essentially I have
something like this:
struct foo
{ };
void
bar( foo const & lhs, foo const & rhs )
|
by: Nikola |
last post by:
Hello,
I'm writing a String class for C++ and I'm getting the following error
message when using operator:
test.cpp: In function ‘int main()’:
test.cpp:7: error: ISO C++ says that these are...
|
by: mathieu |
last post by:
Could someone please tell me what is wrong with the following -ugly-
piece of c++ code. Why when I explicititely set the template parameter
my gcc compiler start getting confused:
bla.cxx: In...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |