Re: explicit conversion

From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 11/11/04


Date: Thu, 11 Nov 2004 02:38:59 GMT


"তন্ময়
ভট্টাচার্য্য"
<tanmoy@mindspring.com> wrote...
> Victor Bazarov <v.Abazarov@comAcast.net> wrote in message
> news:<oytkd.8470$Ae.2254@newsread1.dllstx09.us.to.verio.net>...
> (Parts snipped without indication)
> [...]
>> Making constructors explicit forces somebody to write 'Type(arg)' instead
>> of letting the compiler figure out that 'Type' should be constructed from
>> the 'arg'.
>
> And allows you to write Type x(arg), though not Type x = arg.

Right.

>> So, making 'Type::operator Type2()' to be explicit does not
>> miraculously allow _some_ conversions while disallowing other.
>
> The semantics I desire is for explicit Type::operator Type2() (or
> explicit operator ::Type2(Type) if such were to be allowed instead) to
> behave identically to explicit Type2::Type2(Type).

I am not sure I follow. This is how it is now:

  struct Type {};
  struct Type2 {
      explicit Type2(Type);
  };

  Type arg;
  Type2 x(arg);
  Type2 y = arg; // error -- no conversion exists

So, now replacing the constructor in Type2 with the cast in Type

  struct Type2 {};
  struct Type {
      explicit operator Type2();
  };

should allow

  Type arg;
  Type2 x(arg); // OK
  Type2 y = arg; // error

, right? What I don't understand is how the conversion operator's being
explicit would affect the construction of Type2. Essentially, the two
forms

  Type2 x(arg);

and

  Type2 y = arg;

are using the same constructor, Type2(Type2 const&), provided by the
compiler, where the reference is bound to a temporary object created from
'arg'. There is no other constructor to use, is there? So, they both
are like

   Type2 const& rtemp(arg.operator Type2());
   Type2 z(rtemp);

and if we forego the creation of the temporary (as allowed), they both
are equivalent to

   Type2 z(arg.operator Type2());

How are you going to make the compiler distinguish between 'Type2 x(arg)'
and 'Type2 x = arg', when the [other part of the ] language says that they
are the same? Conclusion: introduction of 'explicit' type conversion ops
will require other fundamental changes to the language, which will most
likely affect _tons_ of existing programs. Besides, it's unclear that such
change _can_ be done.

.Shrug.

I hope you find plenty of more logical explanations in the other threads
on Google.

Victor



Relevant Pages