473,394 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Ambiguous operator overload?

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:
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. public:
  4.   A() { }
  5.   template <typename T> A(T t) { }
  6. };
  7.  
  8. template <typename T>
  9. A
  10. operator-(const A& lhs, T rhs)
  11. { return A(); }
  12.  
  13. class B
  14. {
  15. public:
  16.   B() { }
  17.  
  18.   B operator-(unsigned int rhs) { return B(); }
  19. };
  20.  
  21. int
  22. main()
  23. {
  24.   B b;
  25.   b - 1;
  26.   return 0;
  27. }
  28.  
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?
Jan 1 '09 #1
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).
Jan 1 '09 #2
weaknessforcats
9,208 Expert Mod 8TB
Your class A needs to be a template.

Expand|Select|Wrap|Line Numbers
  1. template<class T>  
  2. class A 
  3. public: 
  4.   A() { } 
  5.   template <typename T> A(T t) { } 
  6.  
  7. }; 
  8.  template<class T> 
  9. A<T> operator-(const A<T>& lhs, T rhs) 
  10. { return A(); } 
  11.  
As initially written the compiler can't tell whether to use the inline function of class B:
Expand|Select|Wrap|Line Numbers
  1. B operator-(unsigned int rhs) { return B(); } 
  2.  
or to use
Expand|Select|Wrap|Line Numbers
  1. template <typename T> 
  2. operator-(const A& lhs, T rhs) 
  3. { return A(); } 
  4.  
where T is a object of class B or whether to use

By making class A a template, the ambiguity is resolved since
Expand|Select|Wrap|Line Numbers
  1. B operator-(unsigned int rhs) { return B(); } 
  2.  
is a better conversion than
Expand|Select|Wrap|Line Numbers
  1.  template<class T> 
  2. A<T> operator-(const A<T>& lhs, T rhs) 
  3. { return A(); } 
  4.  
where T is an object class B.
Jan 1 '09 #3
@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:
Expand|Select|Wrap|Line Numbers
  1.  template<class T, class U> 
  2. A<T> operator-(const A<T>& lhs, U rhs) 
  3. { return A<T>(); } 
  4.  
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?
Jan 1 '09 #4
weaknessforcats
9,208 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. template <typename T> 
  2. operator-(const A& lhs, T rhs) 
  3. { return A(); } 
  4.  
The lhs argument could obtained as an A object intialized by a B object using the template A constructor.

Or the compiler could call:
Expand|Select|Wrap|Line Numbers
  1. class B 
  2. public: 
  3.   B() { } 
  4.  
  5.   B operator-(unsigned int rhs) { return B(); } 
  6. }; 
  7.  
Hence the ambiguity.

When class A is a template then class B is preferred to A<T>.
Jan 2 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
7
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...
3
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,...
9
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...
11
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); }
3
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...
9
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 )
8
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...
3
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.