Can some tell me why the chooses the constructor in class B over operator B
in class A? Is this not ambiguous?
Thanks.
#include <iostream>
using namespace std;
struct A;
struct B {
B() {}
B(const A&) {} // this is choosen
};
struct A {
A() {}
operator B() const {return B();} // this is not
};
int main()
{
A a;
B b;
b = a; // ambiguous?
}
Jul 23 '05
16 1771
REH wrote: "Ali ehreli" <ac******@yahoo .com> wrote in message news:3b******** *****@individua l.net...
Get a newer gcc; version 3.4.2 spots the ambiguity.
Um, I *am* using 3.4.2. What is your targetted for?
It turns out, the compiler option that catches this is
-pedantic
Ali
"Ali Çehreli" <ac******@yahoo .com> wrote in message
news:3b******** *****@individua l.net... REH wrote: "Ali ehreli" <ac******@yahoo .com> wrote in message news:3b******** *****@individua l.net...
Get a newer gcc; version 3.4.2 spots the ambiguity.
Um, I *am* using 3.4.2. What is your targetted for?
It turns out, the compiler option that catches this is
-pedantic
Ali
Thanks. I assumed that "-ansi" would catch it, but it does not.
REH wrote: That's interesting. Even compiling with all warnings, GCC failed to complain.
C:\c>g++ -std=c++98 -pedantic-errors -Wall temp.cpp -o temp.exe
temp.cpp: In function `int main()':
temp.cpp:22: error: conversion from `A' to `const B' is ambiguous
temp.cpp:14: note: candidates are: A::operator B() const
temp.cpp:9: note: B::B(const A&)
C:\c>
--
Ioannis Vranos http://www23.brinkster.com/noicys
REH wrote: Thanks. I assumed that "-ansi" would catch it, but it does not.
The complete g++ options that I am using may help:
-std=c++98 -pedantic-errors -Wall -O3 -ffloat-store -mtune=pentium3
--
Ioannis Vranos http://www23.brinkster.com/noicys
"Ioannis Vranos" <iv*@remove.thi s.grad.com> wrote in message
news:1112248707 .384234@athnrd0 2... REH wrote:
Thanks. I assumed that "-ansi" would catch it, but it does not.
The complete g++ options that I am using may help:
-std=c++98 -pedantic-errors -Wall -O3 -ffloat-store -mtune=pentium3 -- Ioannis Vranos
http://www23.brinkster.com/noicys
Thanks. As another poster informed me, it was "-pedantic" that does the
trick. Though, I don't understand why you have to utilize an option to get
GCC to report this as an error.
REH
REH wrote: Thanks. As another poster informed me, it was "-pedantic" that does the trick. Though, I don't understand why you have to utilize an option to get GCC to report this as an error.
GCC considers itself smarter than this, I guess. :-) Or in other words, it considers that
the copy (=conversion) constructor of the type on the left that is assigned the value of
the type on the right, makes sense to be used (or in other words, it considers that type
conversions (copy constructors) have more priority than compatibility operators).
Theoretically speaking, both should have the same end result. But I think no one would
define both of them on purpose, so here we are. :-)
--
Ioannis Vranos http://www23.brinkster.com/noicys
"Howard" <al*****@hotmai l.com> wrote in message news:<s_******* ************@bg tnsc04-news.ops.worldn et.att.net>... I think the answer is given in Stroustrup's "The C++ Programming Language", p277: "User-defined conversions are considered only if they are neccessary to resolve a call." I don't have the Standard to back up that statement, but if he's correct, then the fact that a copy-constructor exists that can handle the job negates the need to bother looking at other alternatives. Thus, no ambiguity is encountered.
-Howard
The Standard says, in 12.3-2:
User-defined conversions are applied only where they are unambiguous
(10.2, 12.3.2).
Thus, I believe it's an error to provide the two conversions, because
they are always ambigous and thus could not be applied.
On the other hand, The Standard says, in 12.3.2-1:
"... Classes, enumerations, and typedef-names shall not be declared in
the typespecifier-seq. ..."
So I believe that the conversion operator to B is invalid.
I may be interpreting The Standard wrongly, though.
Regards,
Marcelo Pinto. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alex Vinokur |
last post by:
Why is it ambiguous?
------ foo.cpp ------
struct Foo
{
Foo operator* (Foo) { return Foo(); }
Foo operator* (int) const { return Foo(); }
Foo () {}
Foo (int) {}
};
|
by: Alan Johnson |
last post by:
Consider the following code, with the interesting lines numbered in
comments:
class A
{
public :
bool f(int level = 1) // line 5
{ return true ; }
|
by: xuatla |
last post by:
compile error:
test1.cpp:21: error: ISO C++ says that
`T mtd::CDiffOperator::getdp(const mtd::mVector&, long int,
mtd::mBCTYPE) const'
and
`void mtd::CDiffOperator::getdp(mtd::mVector&, const mtd::mVector&,
mtd::mBCTYPE) const'
are ambiguous even though the worst conversion for the former is better
than the worst conversion for the latter
|
by: Lee Gillie |
last post by:
I have a VS6 project which I brought into VS .NET, and all has been
building fine. Then I upgraded to VS 2003 and I have one source which
will no longer compile. Any clues?
Compiling...
DotNetManagedExport.cpp
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\atlmfc\include\atlcom.h(5529) : error C2872: 'CONNECTDATA' :
ambiguous symbol
could be 'C:\Program Files\Microsoft Visual Studio .NET
|
by: Michi Henning |
last post by:
Hi,
the following code produces an error on the second-last line:
Interface Left
Sub op()
End Interface
Interface Right
Sub op(ByVal i As Integer)
| |
by: Johnny van Cadsand |
last post by:
Hi,
I get the following errors when trying to build my solution:
'Exception' is ambiguous in the namespace 'System'
'EventArgs' is ambiguous in the namespace 'System'
'IDisposable' is ambiguous in the namespace 'System'
I know it has something to do with double references, but i can't find
anything... I think it's weird because it's in the namespace 'System'
|
by: --== Alain ==-- |
last post by:
Hi,
Under VC++.net, if i include windows.h, and compile my application, i
have several data types ambiguous, like IDataObject, IMessageFilter,
IDropTarget,...
so it means that they are defined in windows.h but also in some other
namespaces.
how can i do to still have my windows.h included and avoiding ambiguous
|
by: rn5a |
last post by:
This is the second time I am asking this question in this newsgroup
since I haven't got a solution or response from anyone in my previous
post & I need to resolve this issue desperately. Sorry for the double
post but I just didn't have any other option other than re-posting the
same question in this newsgroup.
Consider the following code in a VB class file:
Namespace LoginUserFetchDB
Public Class ZForZebra : Inherits SoapHeader
|
by: Anna Smidt |
last post by:
I am having an "ambiguous call to overloaded function" error again.
This is the function:
int nGetProfWidth (int ncols, unsigned ProfSpec)
{
if ((ProfSpec & PROF_2d) == 0)
return ncols;
return int(sqrt(ncols)); //HERE THE ERROR IS THROWN
}
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |