Topic: Contextual convertibility and logical operators


Author: Inconnu <shirarenai@yandex.ru>
Date: Tue, 3 Nov 2009 12:09:05 CST
Raw View
Is the following correct, according to the upcoming standard?

struct A
{
     explicit operator int () {...}
};

A a;

bool b = a && true;

-------------------
If it is not correct (I think it should not be correct, according to
the rules of overloading resolution for operators), then it is not
clear, why in 5.15.1 (as well as for other logical operators) the
wording was changed from "implicitly converted" to "contextually
converted". Are there examples, which are correct only with the new
wording?

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Tue, 3 Nov 2009 17:07:48 CST
Raw View
Inconnu wrote:

> Is the following correct, according to the upcoming standard?
>
> struct A
> {
>      explicit operator int () {...}
> };
>
> A a;
>
> bool b = a && true;
>


> -------------------
> If it is not correct (I think it should not be correct, according to
> the rules of overloading resolution for operators), then it is not
> clear, why in 5.15.1 (as well as for other logical operators) the
> wording was changed from "implicitly converted" to "contextually
> converted". Are there examples, which are correct only with the new
> wording?
>
Yes, a narrow reading indicates that the code is not correct, because
overload resolution is based on converting arguments to parameters using
implicit conversions defined by copy initialization. I've noted it here:
http://groups.google.com/group/comp.std.c++/msg/2e1ed2fbf4a03ac3 . As the
whole discussion there indicates, this matter is currently underspecified in
the Standard in my opinion - this "contextually converted" apparently is
there for user defined types, and subtly interacts with overload resolution.

Not sure whether i got this right yet, but IMHO there should be a section in
13.6 that explains implicit conversion sequences for arguments of builtin
operators: This should forbid conversions of non-class types not listed in
clause 5 to convert to suitable operand types, and should also make up the
special rules for operator!, operator&& and so on. A note can be made to
refer to it from 13.3.3.1 or a sub-section of it, like "[Note: For builtin-
candidates, special rules apply. See 13.6]". IMHO, the "patch" should also
clarify 5/3 - i think it's not clear at all what exactly it says.

Anyway, clang accepts the code above, and GCC too, both with c++0x mode
enabled.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: CornedBee <wasti.redl@gmx.net>
Date: Thu, 5 Nov 2009 16:26:57 CST
Raw View
On Nov 3, 7:09 pm, Inconnu <shirare...@yandex.ru> wrote:
> Is the following correct, according to the upcoming standard?
>
> struct A
> {
>      explicit operator int () {...}
>
> };
>
> A a;
>
> bool b = a && true;
>

Yes.

> If it is not correct (I think it should not be correct, according to
> the rules of overloading resolution for operators), then it is not
> clear, why in 5.15.1 (as well as for other logical operators) the
> wording was changed from "implicitly converted" to "contextually
> converted". Are there examples, which are correct only with the new
> wording?

"Contextually converted" is a special wording introduced specifically
for making explicit conversions useful in a boolean context. The
difference is that for an implicit conversion, the declaration
T t = e;
must be well-formed; for contextual conversion, the declaration
bool t(e);
must be well-formed (contextual conversion is always to bool), i.e.
explicit conversion operators are considered.

The effect is that any explicit conversion operator to a type that has
a standard conversion to bool allows the type to be used in boolean
context. Boolean context is the conditions of if, while, do..while and
for, the arguments to logical AND, OR and NOT, the condition of the
conditional operator and the condition of static_assert.

Sebastian


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]