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

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

我有一个很大的整数 class,我正在努力使它符合 std::showbasestd::noshowbase。此处,"honor" 表示控制整数 class(而不是 C++ standard behaviors)中定义的后缀的使用:

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;