473,804 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Just another difference between compilers.

Hi,

In my code I use two implicit casts in a row. VC compiled it in all
version I have. 7.1 and 8.0, but it seems not to be standard. Comeau in
strict mode and gcc don't accept it.

Of course, I can just throw a cast in C c = (C)b;

but this is not what I want. I want to enable implicit conversion.

Any idea?

Ingo

// main.cpp:

class A{};
class B
{
public:
operator A( )
{
return a;
}
A a;
};
class C
{
public:
C( A const& a ) { }
};
int main()
{
B b;
C c = b;
}

Jun 11 '06 #1
4 1587
nutty wrote:
Hi,

In my code I use two implicit casts in a row. VC compiled it in all
version I have. 7.1 and 8.0, but it seems not to be standard. Comeau in
strict mode and gcc don't accept it.

Of course, I can just throw a cast in C c = (C)b;

but this is not what I want. I want to enable implicit conversion.

Any idea?

Ingo

// main.cpp:

class A{};
class B
{
public:
operator A( )
{
return a;
}
A a;
};
class C
{
public:
C( A const& a ) { }
};
int main()
{
B b;
C c = b;
}


Very strange.
On my GNU compiler, it compiles if you use the following sysntax:
int main()
{
B b;
C c(b);

But technically, both syntax should evaluate to the same thing.
C c(b);
C c = b;

The compiler should fail with both, or accept both.

Jun 11 '06 #2
* Axter:
C c(b);
C c = b;

The compiler should fail with both, or accept both.


The declaration

C c(b);

is a "direct initialization" and involves one user-defined conversion
before c is reached: from B to A (the C constructor argument type).

C c = b;

is a "copy initialization" and involves two user-defined conversions
before c is reached: from B to A, and from A to C, because the right
side is a C object that c is copy-constructed from (although that copy
construction can and usually will be optimized away, it's there).

The standard allows only one implicit user-defined conversion, so that
the compiler won't outsmart the programmer (at least not a genius one).

You have the same situation with std::auto_ptr.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 11 '06 #3
The standard allows only one implicit user-defined conversion, so that
the compiler won't outsmart the programmer (at least not a genius one).

You have the same situation with std::auto_ptr.


And in my real code it is actually a situation similar to auto_ptr,
where I use a reference counting pointer class. The pointer class
should be assigned the internal value of a proxy object, which is a raw
pointer.

So, next question: How do people resolve the issue with the auto ptr? I
mean, almost any issues that can be thought of, have been solved at
some time by somebody, right? :-)

Jun 12 '06 #4
Very strange.
On my GNU compiler, it compiles if you use the following sysntax:
int main()
{
B b;
C c(b);

But technically, both syntax should evaluate to the same thing.
C c(b);
C c = b;

The compiler should fail with both, or accept both.


by technicaly, you mean the generated instructions.

Of course, in this case I could write it like this. But then I have
peaces of code, where an assignment is affected, or an argument to a
function:

void func( C c );

func( b );

and

C c;
c = b;

in these cases I should either write

func( (C)b );
or
func( C(b) );

and
c = (C)c;
or
c = C( b );

That would surely work, but is actually an explicit cast.

The reason for my concern is, that I don't want to think and care that
there are casts involved. Also casts are even more powerful and may do
things that I don't want in some situations. They may invoke a
cunstructer which is declared explicit, where for some reason a
function returns the exact type that matches the cunstructor parameter.

Jun 12 '06 #5

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

Similar topics

9
2585
by: lomat | last post by:
Hello, I am using VisiBroker 4.0 on RedHat 8.0 with GCC 3.2. When I do a "nm liborb_r.so | grep _invoke", I get some output. The symbol of my intrest is manged as below, 002f6270 T _preinvoke__12CORBA_ObjectPCc Now when I build an app that links to liborb_r.so, and uses the _preinvoke API, GCC mangles this symbol as follows,
4
10012
by: Nimmi Srivastav | last post by:
Once and for all can someone kindly tell me the difference between C and C++ linkage. I thought I understood it till someone showed me the other day that C functions, that would ordinarily require the extern "C" declaration, for correct linkage into a C++ program, don't need it if the filename, in which they are defined, ends in .cc or .cpp! Can anyone explain this in a rational manner? For the record, this is what I have heard...
6
433
by: Sonia | last post by:
Why does C++ return a 0 and Visual C++ return a 1 when I execute this program? Why the difference if they are both MS products? #include <iostream> using namespace std; void main() { cout<<"123"<="89"<<endl;
16
16296
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
1
2532
by: xman | last post by:
very hard is fix exactly group for this question.. but I am sure that some of people here have some experience with these compilers: Visual Studio NET Enterprise Architect 2002 and Visual Studio NET 2003 1. What is difference between Visual Studio NET Enterprise Architect 2002 and Visual Studio NET 2003 2. If I try user Visual Studio NET 2003, what I have not included inside (in compare with Visual Studio NET 2003)?
66
11822
by: Jason Curl | last post by:
I've seen the document N869.txt and the copy I could find talks about C99. Is there anything that comprehensively describes the difference between C89 and C99? I'd like to write code to be as portable as possible. Thanks, Jason.
3
1757
by: Chen Shusheng | last post by:
Hello, I do a experiment on compile one file using different compilers.I got the results below: Output file1: hello1 14,332Byte under C-free Output file2: hello2 4,096Byte under Dev-C++ Output file3: hello3 57,344Byte under Microsoft Visual C++ Express 2005 Why does these files vary so large? Is there any method and tools of analyzing these .EXE files to see its difference?
31
2944
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes statements that the compiler does not process. The good people in the alt.comp.lang.learn.c-c++ newsgroup insist that the preprocessor is just one of many passes. The preprocessor processes a grammer unique to the preprocessor and only that grammer. ...
45
7402
by: anto frank | last post by:
hi friends, is ther any difference in array in c and array in c++?
15
2420
by: puzzlecracker | last post by:
I see that a lot of former in the code, and wonder if there is a technical reason for that
0
9596
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
10604
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...
0
10103
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
9179
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...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
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
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4316
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
3
3006
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.