Title
"Convertible to bool" requirement in conjunction and disjunction
Status
c++17
Section
[meta.logical]
Submitter
Tim Song

Created on 2016-01-18.00:00:00 last changed 77 months ago

Messages

Date: 2017-12-05.03:48:37

Proposed resolution:

The resolution for this issue was combined with the resolution for LWG 2567, so 2567 resolves this issue here as well.

Date: 2017-12-05.03:48:37

[ Dec 2017 - The resolution for this issue shipped in the C++17 standard; setting status to 'C++17' ]

Date: 2016-08-03.00:00:00

[ 2016-08-03 Chicago LWG ]

Walter, Nevin, and Jason provide initial Proposed Resolution.

Previous resolution [SUPERSEDED]:

This wording is relative to N4606.

  1. Change [meta.logical] as indicated:

    template<class... B> struct conjunction : see below { };
    

    […]

    -3- The BaseCharacteristic of a specialization conjunction<B1, ..., BN> is the first type Bi in the list true_type, B1, ..., BN for which Bi::value == false! bool(Bi::value), or if every Bi::value != falsebool(Bi::value), the BaseCharacteristic is the last type in the list. […]

    -4- For a specialization conjunction<B1, ..., BN>, if there is a template type argument Bi with Bi::value == false! bool(Bi::value), then instantiating […]

    template<class... B> struct disjunction : see below { };
    

    […]

    -6- The BaseCharacteristic of a specialization disjunction<B1, ..., BN> is the first type Bi in the list false_type, B1, ..., BN for which Bi::value != falsebool(Bi::value), or if every Bi::value == false! bool(Bi::value), the BaseCharacteristic is the last type in the list. […]

    -7- For a specialization disjunction<B1, ..., BN>, if there is a template type argument Bi with Bi::value != falsebool(Bi::value), then instantiating […]

    template<class B> struct negation : bool_constant<!bool(B::value)> { };
    

    -8- The class template negation forms the logical negation of its template type argument. The type negation<B> is a UnaryTypeTrait with a BaseCharacteristic of bool_constant<!bool(B::value)>.

Date: 2016-01-18.00:00:00

The specification of conjunction and disjunction in [meta.logical] p2 and p5 requires Bi::value to be convertible to bool, but nothing in the specification of the actual behavior of the templates, which instead uses the expressions Bi::value == false and Bi::value != false instead, actually requires this conversion.

If the intention of this requirement is to allow implementations to pass Bi::value directly to std::conditional, like the sample implementation in P0013R1:

template<class B1, class B2>
struct and_<B1, B2> : conditional_t<B1::value, B2, B1> { };

then it's insufficient in at least two ways:

  1. Nothing in the specification requires the result of comparing Bi::value with false to be consistent with the result of the implicit conversion. This is similar to LWG 2114, though I don't think the BooleanTestable requirements in that issue's P/R covers Bi::value == false and Bi::value != false.

  2. More importantly, the above implementation is ill-formed for, e.g., std::conjunction<std::integral_constant<int, 2>, std::integral_constant<int, 4>>, because converting 2 to bool is a narrowing conversion that is not allowed for non-type template arguments (see [expr.const]/4). (Note that GCC currently doesn't diagnose this error at all, and Clang doesn't diagnose it inside system headers.) It's not clear whether such constructs are intended to be supported, but if they are not, the current wording doesn't prohibit it.

History
Date User Action Args
2017-12-05 03:48:37adminsetmessages: + msg9577
2017-12-05 03:48:37adminsetstatus: open -> c++17
2016-08-07 11:32:53adminsetstatus: new -> open
2016-08-04 19:26:57adminsetmessages: + msg8402
2016-08-04 19:26:57adminsetmessages: + msg8401
2016-01-18 00:00:00admincreate