How does istream::operator>>( const char& ) as no such function is implemented?

我只是一个虾纸丫 提交于 2019-12-01 23:03:54

问题


Looking at istream documentation, you will see that there is no implementation of function istream &operator>>( char& ), but if you compile and run the code below, it will work just as expected.

#include<iostream>

int main( ) {

  char c;

  std::cin >> c;

  std::cout << c << std::endl;

  return( 0 );

}

Given cin is an object for class istream, which specialization of operator>> is called when std::cin >> c; is executed?


回答1:


operator>> also is implemented as non-member functions.

istream& operator>> (istream& is, char& c)




回答2:


As stated here, operator>> is also implemented as non-member-function of cin.

Non-member function :

istream& operator>>( istream& st, char& ch );

There is always the standard stated it explicitly in the section § 27.7.2.2.3 :

27.7.2.2.3 basic_istream::operator>> [istream::extractors]

11/ Returns: in.

    template<class charT, class traits> basic_istream<charT,traits>& operator>>
        (basic_istream<charT,traits>& in, charT& c);

    template<class traits> basic_istream<char,traits>& operator>>
        (basic_istream<char,traits>& in, unsigned char& c);

    template<class traits> basic_istream<char,traits>& operator>>
        (basic_istream<char,traits>& in, signed char& c);


来源:https://stackoverflow.com/questions/18404015/how-does-istreamoperator-const-char-as-no-such-function-is-implemented

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!