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

时间:2015-03-30 17:25:20

标签: c++ ostream iomanip

我有一个大整数类,我试图让它荣誉std::showbasestd::noshowbase。在这里,"荣誉"意味着控制Integer类中定义的后缀的使用(而不是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

1 个答案:

答案 0 :(得分:1)

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

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

return out;