473,839 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Precedence of overloaded cast operators

I have a problem regarding the precedence of overloaded cast operators.
My program is as follows:

#include <stdio.h>

class A;

class B {
public:
B(A &a) {
printf("constru ct\n");
}
};

class A {
public:
B f() {
printf("f\n");
return(B(*this) );
}

operator B () {
printf("convert \n");
return(f());
}
};

int main() {
A a;
a.f();
return(0);
}

When I compile and run the program on Linux using g++, the output is:

f
construct

This is pretty much what I'd expect. However, when I do the same on a
HP-UX system using aCC, the output is:

f
convert
f
convert
f
convert
f
convert
....

This continues until the stack overflows. It seems that the overloaded
cast operator has higher precedence than the contructor of B. I
appreciate this isn't the place to ask about specific C++ compilers, so
my question is: am I making unreasonable assumptions about what the
correct behaviour of C++ should be in these situations?

Thanks in advance,
Ross

Oct 31 '06 #1
3 2141
Ross wrote:
I have a problem regarding the precedence of overloaded cast
operators. My program is as follows:

#include <stdio.h>

class A;

class B {
public:
B(A &a) {
printf("constru ct\n");
}
};

class A {
public:
B f() {
printf("f\n");
return(B(*this) );
}

operator B () {
printf("convert \n");
return(f());
}
};

int main() {
A a;
a.f();
return(0);
}

When I compile and run the program on Linux using g++, the output is:

f
construct

This is pretty much what I'd expect. However, when I do the same on a
HP-UX system using aCC, the output is:

f
convert
f
convert
f
convert
f
convert
...

This continues until the stack overflows. It seems that the overloaded
cast operator has higher precedence than the contructor of B. I
appreciate this isn't the place to ask about specific C++ compilers,
so my question is: am I making unreasonable assumptions about what the
correct behaviour of C++ should be in these situations?
5.2.3 requires that the expression B(*this) is interpreted as a c-tor
invocation (construction of a value of type B). So, I think HP's aCC
is wrong.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 31 '06 #2
Victor Bazarov:
5.2.3 requires that the expression B(*this) is interpreted as a c-tor
invocation (construction of a value of type B). So, I think HP's aCC
is wrong.

Are you saying the following?

When the following syntax is encountered:

A(obj_b)

, the compiler shall search for a constructor for A before it searches for
a conversion operator for B?

The following compiles OK for me with g++:

class A {};

class B {
public:

operator A() const
{
return A();
}
};

int main()
{
A obj_a;
B obj_b;

obj_a = obj_b;

obj_a = A(obj_b);
}

Even though the "function call syntax" or whatever you want to call it is
used, the conversion operator is still invoked. But are you saying that a
constructor must be sought before a conversion operator is sought?

--

Frederick Gotham
Oct 31 '06 #3
Frederick Gotham wrote:
Victor Bazarov:
>5.2.3 requires that the expression B(*this) is interpreted as a c-tor
invocation (construction of a value of type B). So, I think HP's aCC
is wrong.


Are you saying the following?

When the following syntax is encountered:

A(obj_b)

, the compiler shall search for a constructor for A before it
searches for a conversion operator for B?

The following compiles OK for me with g++:

class A {};

class B {
public:

operator A() const
{
return A();
}
};

int main()
{
A obj_a;
B obj_b;

obj_a = obj_b;

obj_a = A(obj_b);
}

Even though the "function call syntax" or whatever you want to call
it is used, the conversion operator is still invoked. But are you
saying that a constructor must be sought before a conversion operator
is sought?
Yes. However, in this case A(const A&) is the constructor invoked,
and the reference is the one bound to a temporary returned from the
conversion operator ('B::operator A()'). Try adding a parameterised
constructor to A and making it private.

class A { A(class B const&); public: A(); }; // I changed this

class B {
public:
operator A() const { return A(); }
};

int main()
{
A obj_a;
B obj_b;

obj_a = obj_b; // now it's ambiguous
obj_a = A(obj_b); // now it cannot access the c-tor
}

AFAICT, in the original variation both cases _can_ be made equivalent
by the compiler because it is allowed to eliminate extra temporaries.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 31 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3328
by: Andrew Koenig | last post by:
It has been pointed out to me that various C++ books disagree about the relative precedence of ?: and the assignment operators. In order to satisfy myself about the matter once and for all, I looked at the grammar in the C++ standard. The relative fragments are as follows: conditional-expression: logical-or-expression logical-or-expression ? expression : assignment-expression
47
2712
by: Jeff Relf | last post by:
Hi All, I plan on using the following C++ code to create nodes with unlimited children: // I would like to declare NodeT like this, // but it won't compile because Lnk_T is not defined yet. struct NodeT { Lnk_T Lnk ; };
3
2395
by: Sensorflo | last post by:
After browsing though many newsgroups articels I'm still not shure how operator precedence, operator associativity, sequence points, side effects go together. Currently I have the following view: An expression a = b() + c() * d++; can be transformed with the rules of operator associativity and operator precedence into a tree
25
1729
by: noe | last post by:
Hello, I'm writing a file system filter driver and I've found in an example this sentence: if (VALID_FAST_IO_DISPATCH_HANDLER( fastIoDispatch, FastIoRead )) { return (fastIoDispatch->FastIoRead)( FileObject, FileOffset, Length, Wait,
36
2822
by: Frederick Gotham | last post by:
sizeof has the same precedence as a cast, and they both bind from right to left. The following won't compile for me with gcc: int main(void) { sizeof(double)5; return 0; }
7
1793
by: lovecreatesbea... | last post by:
c-faq, 4.3: The postfix ++ and -- operators essentially have higher precedence than the prefix unary operators. BSD Manual, OPERATOR(7): Operator Associativity -------- ------------- ! ~ ++ -- - (type) * & sizeof right to left Is there a complete table on operators, their precedence and
5
2545
by: junky_fellow | last post by:
Hi, I have a very basic doubt about associativity and precedence of operators. I am not a computer science person and may find it quite weird. Consider an expression 4+5*2
7
1961
by: linq936 | last post by:
Hi, I am puzzled on this C puzzle: How to interpret this C statement: sizeof (int) * p Is that the size of integer type multiplies p or the size of whatever p points to in integer type? I think it should be the latter because there are 4 parser tokens here, sizeof, int, * and p, the first and the second are of same precedence which are higher than the third and all three operators are
7
1854
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 5 - Expressions * exercises 5.1 and 5.2 */ #include <iostream>
0
9855
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, 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...
0
9697
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10908
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, 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...
1
10648
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,...
0
10293
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 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...
0
9426
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7017
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();...
0
5682
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...
2
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.