3

I am switching over compilers from GCC to Clang/LLVM and running into compilation errors I didn't experience before.

I have a class that looks something like this:

#include <iostream>

class foo {
    public:
        bar(std::istream& is) : _fp(is), _sCheck(is != std::cin) { /* ... */ }
    private:
        std::istream& _fp;
        bool _sCheck;
}

When I compile this file, I get the following error with clang++, where the initialization of the private variable _sCheck fails:

error: invalid operands to binary expression ('std::istream' (aka 
'basic_istream<char>') and 'istream' (aka 'basic_istream<char>'))

  (is != std::cin)
   ~~ ^  ~~~~~~~~

If both objects in this address comparison are of the same type, why is clang++ returning an error, while g++ does not?

I tried a dynamic_cast to make them both std::istream&, but this, too, returned an error:

error: invalid operands to binary expression ('std::istream' (aka 
'basic_istream<char>') and 'std::istream')

(is != dynamic_cast<std::istream&>(std::cin))
 ~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I apologize in advance if this is a dumb question; I appreciate any pointers.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • Have you by any chance switched from C++03 to C++11? – BoBTFish Sep 18 '13 at 09:13
  • Yes, I am using `-std=c++11 -stdlib=libc++` flags. – Alex Reynolds Sep 18 '13 at 09:18
  • My thinking here was that you were previously hitting `operator void *`, which would not be doing what you think, and is replaced in C++11 with `explicit operator bool`. But the error messages don't match up, so I'm not sure now. – BoBTFish Sep 18 '13 at 09:20

1 Answers1

8

You are using references, so you are comparing the objects and not the pointers as you maybe intended to. It seems GCC has an extension which allows you to compare std::istream objects, but this is not part of the standardized interface of std::basic_istream. Try:

_sCheck(&is != &std::cin)
Daniel Frey
  • 55,810
  • 13
  • 122
  • 180