Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: XE8 : ambiguity riddle


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 1 - Last Post: Jan 7, 2016 6:25 AM Last Post By: Josh Kelley
Jan Dijkstra

Posts: 206
Registered: 11/4/99
XE8 : ambiguity riddle  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 7, 2016 4:51 AM
I have a small struct, in which (amongst a lot of others) I have defined the following members and operators:

struct SNumValue
{
 
  __fastcall SNumValue (double source);
  __fastcall SNumValue (TDateTime const &source);
 
  SNumValue & __fastcall operator= (double source);
  SNumValue & __fastcall operator= (TDateTime const &source);
 
  __fastcall operator double (void) const;
  __fastcall operator TDateTime (void) const;
 
};

Now I try to use this. When I write the following
  SNumValue value (TDateTime::CurrentDate ());
 
  TDateTime date = value;

everything compiles just fine. But when I write
  SNumValue value (TDateTime::CurrentDate ());
 
  TDateTime date;
  date  = value;

I get the following error.
[bcc32 Error] licdrng.cpp(556): E2015 Ambiguity between '_fastcall operator System::TDateTime::=(const System::TDateTime &) at f:\radxe8\include\windows\rtl\systdate.h:61' and '_fastcall operator System::TDateTime::=(const double) at f:\radxe8\include\windows\rtl\systdate.h:63'
  Full parser context
    licdrng.cpp(10): namespace licSystem
    licdrng.cpp(552): parsing: void _fastcall licSystem::TLicDateRangeItem::SetNumFrom(const licSystem::SNumValue &)

and my question is: why? Why does the first case compile just fine, but the second case suddenly throws up an ambiguity error for the assignment operator of a TDateTIme
Josh Kelley

Posts: 75
Registered: 3/6/08
Re: XE8 : ambiguity riddle  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 7, 2016 6:25 AM   in response to: Jan Dijkstra in response to: Jan Dijkstra
On 1/7/2016 7:51 AM, Jan Dijkstra wrote:
and my question is: why? Why does the first case compile just fine, but the second case suddenly throws up an ambiguity error for the assignment operator of a TDateTIme

In 10 Seattle, at least, TDateTime defines the following methods:

     __fastcall TDateTime(const TDateTimeBase& src);
     __fastcall TDateTime(const double src);
     TDateTime& __fastcall operator =(const TDateTime& rhs);
     TDateTime& __fastcall operator =(const double rhs);


Your first example looks like assignment, but because it's written where
you're declaring the variable, it invokes TDateTime's copy constructor,
NOT TDateTime's operator=. The const double overload of TDateTime's
copy constructor is a better fit than the const TDateTimeBase& (because
it's only an implicit cast, versus an implicit cast plus
derived-to-base), so no ambiguity exists.

   TDateTime date = value;


Your second example invokes operator=, which has two equally good
overloads (both involve a single implicit cast), so it's ambiguous.

--
Josh Kelley
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02