Title
std::cout << &X::f prints 1
Status
nad
Section
[ostream.formatted]
Submitter
Peter Dimov

Created on 2022-01-31.00:00:00 last changed 27 months ago

Messages

Date: 2022-08-23.13:55:25

Proposed resolution:

This wording is relative to N4901.

  1. Modify [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;
    }
    
Date: 2022-08-23.00:00:00

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

Date: 2022-03-15.00:00:00

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

Needs a paper to LEWG if anything should change here.

Date: 2022-01-31.00:00:00

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 [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.

History
Date User Action Args
2022-08-23 13:55:25adminsetmessages: + msg12681
2022-03-04 14:32:50adminsetmessages: + msg12388
2022-03-04 14:32:50adminsetstatus: new -> nad
2022-02-05 13:04:51adminsetmessages: + msg12338
2022-01-31 00:00:00admincreate