【问题标题】:How to test for std::showbase or std::noshowbase in output operator?如何在输出运算符中测试 std::showbase 或 std::noshowbase?
【发布时间】:2023-04-01 20:01:02
【问题描述】:

我有一个很大的整数类,我正在努力让它尊重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

【问题讨论】:

  • 你的意思是out.flags() &amp; std::ios_base::showbase

标签: c++ ostream iomanip


【解决方案1】:

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

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

return out;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 2019-12-06
    相关资源
    最近更新 更多