🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

3 total messages Started by Roman Lechtchinsky Mon, 18 Mar 1996 08:02
Constructors and conversion operator
#3706
Author: Roman Lechtchinsky
Date: Mon, 18 Mar 1996 08:02
73 lines
1835 bytes
Hi,

I have a question about conversion operators and constructors. Consider the 
following:

class X
{
	X( Y );
};

class Y
{
	Y();

	operator X();
};

void foo( X );

int main()
{
	Y y;
	foo1(y);
	foo1(X(y));
	foo1((X)y);
};

In all three cases my compiler says that the call is ambiguous. Is it 
correct? The DWP says:

	User-defined conversions are used implicitly only if they are 
unambiguous. [class.conv.fct]

What is meant by user-defined conversions - only conversion operators or both 
conversion operator and converting constructors?

Now the second one:

class A
{
		A(B);
};

class B
{
		operator A&();
};


void foo( A )
{}

int main()
{
	B b;

	foo( b );
	foo(A(b));
	foo((A)b);
}

Again, my compiler complains about all three calls. In fact, it complains 
even about A a = A(b). Is this correct?

Bye

Roman
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
Re: Constructors and conversion operator
#3729
Author: Steve Clamage
Date: Tue, 19 Mar 1996 00:18
48 lines
2104 bytes
In article 3438@cs.tu-berlin.de, Roman Lechtchinsky
<wolfro@cs.tu-berlin.de> writes:
>
>I have a question about conversion operators and constructors. Consider the 
>following:
>
>class X
>{
>	X( Y );
>};
>
>class Y
>{
>	operator X();
>};

A constructor taking a single argument is a type conversion operator from
the argument type to the class type. The arrangement you show is always
going to lead to ambiguity errors. There are two ways to convert a Y
to an X, and neither is preferred over the other. Operator conversion
functions should normally be avoided, partly for this reason, and partly
because they can wind up being used in surprising circumstances. The
compiler might find a conversion that allows erroneous code to compile.

>Now the second one:
>
>class A
>{
>		A(B);
>};
>
>class B
>{
>		operator A&();
>};

This is the same situation. There is no syntactic difference between
using a reference or an object. Once again, there are two ways to
make an A out of a B, and neither is preferred.

---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
Re: Constructors and conversion operator
#3733
Author: Roman Lechtchinsky
Date: Tue, 19 Mar 1996 10:24
75 lines
3260 bytes
Steve Clamage wrote:
> 
> In article 3438@cs.tu-berlin.de, Roman Lechtchinsky
> <wolfro@cs.tu-berlin.de> writes:
> >
> >I have a question about conversion operators and constructors. Consider the
> >following:
> >
> >class X
> >{
> >       X( Y );
> >};
> >
> >class Y
> >{
> >       operator X();
> >};
> 
> A constructor taking a single argument is a type conversion operator from
> the argument type to the class type. The arrangement you show is always
> going to lead to ambiguity errors. There are two ways to convert a Y
> to an X, and neither is preferred over the other. Operator conversion
> functions should normally be avoided, partly for this reason, and partly
> because they can wind up being used in surprising circumstances. The
> compiler might find a conversion that allows erroneous code to compile.
> 
> >Now the second one:
> >
> >class A
> >{
> >               A(B);
> >};
> >
> >class B
> >{
> >               operator A&();
> >};
> 
> This is the same situation. There is no syntactic difference between
> using a reference or an object. Once again, there are two ways to
> make an A out of a B, and neither is preferred.
> 

First of all, sorry: there is a mistake in the example, the constructor 
should be A(B&). However, it doesn't matter since both cases are only a 
variation of the following problem:

The DWP defines:

A constructor declared without the  function-specifier  explicit  that
  can  be called with a single parameter specifies a conversion from the
  type of its first parameter to the type of its  class. [class.conv.ctor]

Now, to me this means that a copy constructor is a converting constructor 
since it takes a single parameter which is a reference to an object of the 
class ( I don't think that this is intended ). If this is true a copy 
constructor is a user-defined conversion ( after all, A and A& are different 
types, aren't they ). This implies that the following call to foo:

foo(b)

is ill-formed ( not ambiguous ) since it requires either b=>B(b)=>A(B(b)) or 
b=>A&(b)=>A(A&(b)) but only one user-defined conversion may be applied ( note 
that foo is defined as foo(A), not foo(A&) ). 


Bye

Roman
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads