473,320 Members | 1,867 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,320 software developers and data experts.

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("construct\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 2107
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("construct\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
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...
47
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....
3
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: ...
25
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...
36
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
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...
5
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
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...
7
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 5 - Expressions * exercises 5.1 and 5.2 */ #include <iostream>
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.