Issue 468: unexpected consequences of ios_base::operator void*() (original) (raw)


This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of CD1 status.

468. unexpected consequences of ios_base::operator void*()

Section: 31.5.4.4 [iostate.flags] Status: CD1 Submitter: Martin Sebor Opened: 2004-06-28 Last modified: 2016-01-28

Priority: Not Prioritized

View all other issues in [iostate.flags].

View all issues with CD1 status.

Discussion:

The program below is required to compile but when run it typically produces unexpected results due to the user-defined conversion from std::cout or any object derived from basic_ios to void*.

#include <cassert>
#include <iostream>

int main ()
{
    assert (std::cin.tie () == std::cout);
    // calls std::cout.ios::operator void*()
}

Proposed resolution:

Replace std::basic_ios<charT, traits>::operator void*() with another conversion operator to some unspecified type that is guaranteed not to be convertible to any other type except for bool (a pointer-to-member might be one such suitable type). In addition, make it clear that the pointer type need not be a pointer to a complete type and when non-null, the value need not be valid.

Specifically, change in [lib.ios] the signature of

operator void*() const;

to

operator unspecified-bool-type() const;

and change [lib.iostate.flags], p1 from

operator void*() const;

to

operator unspecified-bool-type() const;

 -1- Returns: if fail() then a value that will evaluate false in a
  boolean context; otherwise a value that will evaluate true in a
  boolean context. The value type returned shall not be
  convertible to int.

 -2- [Note: This conversion can be used in contexts where a bool
  is expected (e.g., an if condition); however, implicit
  conversions (e.g., to int) that can occur with bool are not
  allowed, eliminating some sources of user error. One possible
  implementation choice for this type is pointer-to-member.  - end
  note]

[Redmond: 5-4 straw poll in favor of doing this.]

[Lillehammer: Doug provided revised wording for "unspecified-bool-type".]