Standard Library Applications for Explicit Conversion Operators

Document Number: N2434(07-0304)
2007-10-03
Alisdair Meredith <public@alisdairm.net>

Background and Rationale

One feature of the next version of the C++ Language will be Explicit Conversion operators, as descibed in n2380. One of the key motivating examples is to simplify the 'conversion to unspecified bool' idiom. This paper proposes to replace most use of unspecified-bool-type with explicit bool conversion operators. This will simplify the library clauses and aid clarity by removing pseudo-code from normative signatures and simpliying the associated explanatory text. In particular, note that hole in the type system that 20.5.14.2.6 [func.wrap.func.undef] works around is closed, removing the whole clause.

On the other hand, it deliberately leaves the operator bool() conversion function for the vector<bool> and bitset proxy classes as implicit.

While this paper has no suggested applications of the feature beyond boolean conversions, the committee might want to explore explicit pointer conversion operators for the library smart pointers.

The paper would also resolve LWG issue 644 as NAD.

(A couple of Minor editorial nits (typos etc) are also fixed without comment.)

History

This paper is a revision of N2293. There are two changes:
  • Removed all changes to smart pointers, to be reconsidered once nullptr_t is available. The issue is that the unspecified bool idiom supports a slightly wider range of comparisons that make sense for pointers
  • Removed the update to basic_ios for concern this might break existing code written against an implementation supporting defect report 468. Suggestion is to follow with wording that allows both implementations in this case, under some as-if clause.
  • Proposed Changes to Working Draught (n2284)