3667. std::cout << &X::f prints 1

Section: 31.7.6.3 [ostream.formatted] Status: NAD Submitter: Peter Dimov Opened: 2022-01-31 Last modified: 2022-08-23 13:55:25 UTC

Priority: Not Prioritized

View all issues with NAD status.

Discussion:

At present, the program

#include <iostream>

struct X
{
  void f() {}
};

int main()
{
  std::cout << &X::f;
}

prints 1. That's because member pointers implicitly convert to bool, and there's operator<< overload for bool in 31.7.6.3.2 [ostream.inserters.arithmetic].

This behavior is rarely useful. In C++20, we added deleted overloads to prevent a similar counter-intuitive output for the case in which e.g. L"str" is output to std::cout, which used to print the pointer value using the operator<< overload for const void*.

We should similarly consider adding a deleted overload for member pointers.

[2022-03-04; Reflector poll; Status changed: New → Tentatively NAD]

Needs a paper to LEWG if anything should change here.

[2022-08-23 Status changed: Tentatively NAD → NAD.]

Proposed resolution:

This wording is relative to N4901.

  1. Modify 31.7.6.2.1 [ostream.general], class template basic_ostream synopsis, as indicated:

    namespace std {
      […]
      
      template<class traits>
        basic_ostream<wchar_t, traits>&
          operator<<(basic_ostream<wchar_t, traits>&, const char16_t*) = delete;
      template<class traits>
        basic_ostream<wchar_t, traits>&
          operator<<(basic_ostream<wchar_t, traits>&, const char32_t*) = delete;
          
      template<class charT, class traits, class T, class C>
        basic_ostream<charT, traits>& 
          operator<<(basic_ostream<charT, traits>&, T C::*) = delete;
    }