473,671 Members | 2,208 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 2132
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
3309
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
2669
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
2384
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
1700
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
2789
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
1784
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
2533
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
1946
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
1842
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 5 - Expressions * exercises 5.1 and 5.2 */ #include <iostream>
0
8476
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
8820
jinu1996
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...
0
8670
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
7433
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
5695
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
4224
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...
0
4406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2810
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1809
bsmnconsultancy
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...

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.