By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Just another difference between compilers.

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
* 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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.