如何在输出运算符中测试std::showbase或std::noshowbase

How to test for std::showbase or std::noshowbase in output operator?

本文关键字:std showbase noshowbase 测试 输出 运算符      更新时间:2023-10-16

我有一个大的整数类,我正试图使它尊重std::showbasestd::noshowbase。这里,"荣誉"意味着控制Integer类中定义的后缀的使用(而不是C++标准行为):

std::ostream& operator<<(std::ostream& out, const Integer &a)
{
    ...
    if(out.flags() & std::noshowbase)
        return out;
    return out << suffix;
}

然而,它会导致一个错误:

$ make static
c++ -DDEBUG -g3 -O1 -fPIC -Wno-tautological-compare -Wno-unused-value -DCRYPTOPP_DISABLE_ASM -pipe -c integer.cpp
integer.cpp:3487:17: error: invalid operands to binary expression ('int' and
      'std::ios_base &(*)(std::ios_base &)')
        if(out.flags() & std::noshowbase)
           ~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~
/usr/include/c++/4.2.1/bits/ios_base.h:79:3: note: candidate function not
      viable: no known conversion from 'std::ios_base &(std::ios_base &)' to
      'std::_Ios_Fmtflags' for 2nd argument
  operator&(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
  ^
1 error generated.

我也尝试过有类似错误的std::ios::noshowbasestd::ios_base::noshowbase

如何测试showbasenoshowbase

noshowbase是一个函数,而不是位掩码类型的积分。也没有ios_base::noshowbase。但有ios_base::showbase。也许你想要:

if (out.flags() & std::ios_base::showbase) {
    return out << suffix;
}
return out;