Is the type std :: istream EqualityComparable?

My question will have a logical answer: yes or no. Whatever it is, can someone explain how the following code compiles both GNU-g ++ 4.9.2 and clang 3.5, while GNU-g ++ 5.1.1 no longer accepts it, claiming there is no corresponding operator==?

And how could this be changed for this last compiler to have the same results, i.e. to have operator>>, capable of thus easily distinguishing, is it called by a standard input stream or by something else?

  # include <iostream>
  # include <fstream>

  struct S {};

  std::istream& operator >> (std::istream& i, S& s)
   {
    if(i == std::cin) std::clog << "this is standard input\n";
    else std::clog << "this is some other input stream\n";

    return i;
    }

  int main(int narg, const char ** args)
   {
    S s;
    std :: cin >> s;
    std::ifstream inp(args[1]);
    inp >> s;
    }
  // to be executed with the name of an existing
  // disk file on the command line....
+4
source share
2 answers

No. No operator==, that works with objects std::istream.

std::istream - , std::istream, operator void*, i == std::cin i std::cin void*, . . ++ 11 ( explicit, ), , ++ 11, .

, , i std::cin, &i == &std::cin.

+5

++ ==, >, <, : " a is > , b"?

, istream : , , (, is_open), (, ). string istringstream ifstream, in >> str .

istream, typeid , , . RTTI (typeid)

if ( typeid(in) == typeid(std::cin) )
    // cin
else
   // another istream type

:

std::istream& operator >> (std::istream& i, S& s)
{
     std::clog << "this is standard input\n";   
     return i;
}

std::ifstream& operator>>(std::ifstream& i, S& s)
{
     std::clog << "this is some file input stream\n";
     return i;
}
+1

Source: https://habr.com/ru/post/1616061/


All Articles