0

given the following simple function declaration:

void foo (std::istream& is);

I can call foo in several ways, for example:

fstream f;

foo(std::cin);
foo(f);

is there any way I can check if the given istream& is standard input ? (STD::CIN)

thanks in advance

Rouki
  • 2,239
  • 1
  • 24
  • 41
  • 1
    Not reliable. And that kind of defeats the purpose of using an `istream` argument. Why not just use `std::cin` in your function (if you want to require stdin)? – Elliott Frisch Dec 14 '13 at 07:09
  • Well, `operator==` works on `std::istream`... –  Dec 14 '13 at 07:15
  • I suppose `&is == &std::cin` should work well. – Casey Dec 14 '13 at 07:32
  • Perhaps this helps: http://stackoverflow.com/questions/13172649/ensure-argument-is-an-output-stream-for-the-console – chris Dec 14 '13 at 08:17

1 Answers1

1

You can check using if statements

void foo (std::istream& is)
{
    if(is==std::cin)
    {
        std::cout<<"standard input"<< std::endl;
    }
    else
    {
        std::cout<<"file input"<< std::endl;
    }
}
David Szalai
  • 2,363
  • 28
  • 47
user1061392
  • 304
  • 3
  • 14
  • Please note: It compares the addresses of the underlaying stream buffers via operator void* –  Dec 14 '13 at 10:28