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

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 1574
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
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...
4
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...
6
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() {...
16
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
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...
66
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...
3
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...
31
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...
45
by: anto frank | last post by:
hi friends, is ther any difference in array in c and array in c++?
15
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
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...
1
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: 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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.