本文介绍了如何比较两个std :: istream参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将编译器从GCC切换到Clang / LLVM并运行到我以前没有遇到的编译错误。

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;
}



当我编译此文件时,我收到以下错误: clang ++ ,其中私有变量 _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)
   ~~ ^  ~~~~~~~~

如果这个地址比较中的两个对象都是同一类型,为什么 clang ++ 返回一个错误,而 g ++ 不会?

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

我试过 dynamic_cast $ c> std :: istream& ,但这也返回一个错误:

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.

推荐答案

您正在使用引用,因此您正在比较对象而不是指针, 。看起来GCC有一个扩展,它允许你比较 std :: istream 对象,但这不是标准化接口的一部分。尝试:

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)

这篇关于如何比较两个std :: istream参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 23:50