c++ - Why does <iostream> operator<< pick the apparently wrong overload? -


consider code:

#include <iostream> using namespace std;  class x { public:     operator const wchar_t* () const { return l"hello"; } };  void f(const void *) {     wcout << l"f(const void*)\n"; }  void f(const wchar_t*) {     wcout << l"f(const wchar_t*)\n"; }  int main() {     x x;     f(x);      wcout << x; } 

the output (compiled vs2015 c++ compiler):

f(const wchar_t*) 00118b30 

so seems compiler selects expected const wchar_t* overload f (as there's implicit conversion x const wchar_t*).

however, seems wcout << x picks const void* overload, instead of const wchar_t* 1 (printing address, instead of wchar_t string).

why this?

p.s. know proper way of printing x implement overload of operator<< wostream& operator<<(wostream& , const x&), not point of question.

because when deducing template arguments conversion functions not considered:

// non-template member function. basic_ostream& basic_ostream::operator<<( const void* value );  // template non-member function. template< class chart, class traits > basic_ostream<chart,traits>& operator<<( basic_ostream<chart,traits>& os,                                           const chart* s ); 

the second declaration not consider conversion operator const wchar_t* () const.

i cannot find standard quote, cppreference template argument deduction, implicit conversions says:

type deduction not consider implicit conversions (other type adjustments listed above): that's job overload resolution, happens later.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -