【问题标题】:Why does std::boolalpha ignore field width when using clang?为什么 std::boolalpha 在使用 clang 时会忽略字段宽度?
【发布时间】:2018-03-02 10:40:39
【问题描述】:

以下代码使用 g++ 7 编译器和 Apple clang++ 给出不同的结果。是在使用std::boolalpha时遇到了clang中bool输出对齐的bug,还是我弄错了?

#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>

template<typename T>
void print_item(std::string name, T& item) {
    std::ostringstream ss;
    ss << std::left << std::setw(30) << name << "= " 
       << std::right << std::setw(11) << std::setprecision(5) 
       << std::boolalpha << item << " (blabla)" << std::endl;

    std::cout << ss.str();
}

int main() {
    int i = 34;
    std::string s = "Hello!";
    double d = 2.;
    bool b = true;

    print_item("i", i);
    print_item("s", s);
    print_item("d", d);
    print_item("b", b);

    return 0;
}

区别在于:

// output from g++ version 7.2
i                             =          34 (blabla)
s                             =      Hello! (blabla)
d                             =           2 (blabla)
b                             =        true (blabla)
// output from Apple clang++ 8.0.0
i                             =          34 (blabla)
s                             =      Hello! (blabla)
d                             =           2 (blabla)
b                             = true   (blabla)

【问题讨论】:

  • 有什么区别?感谢 Chiel 的更新。我也可以使用 Clang 6 进行复制。
  • 打印出来也有问题。写结果:
  • @gsamaras。我稍微编辑了示例以更好地显示出了什么问题。
  • 这是LWG 2703
  • @LWG 2703。确实如此。然后我的问题没有解决方案,不幸的是在使用clang时。

标签: c++ c++11 io stringstream ostringstream


【解决方案1】:

在 T.C.提到,这是LWG 2703

设置boolalpha 时不提供填充

N4582 subclause 25.4.2.2.2 [facet.num.put.virtuals] 第 6 段 使 在其行为规范中没有提供填充填充 当 (str.flags() &amp; ios_base::boolalpha) != 0.

因此,我看不到 Clang 的解决方案。但是,请注意:

  • libc++ 完全实现了这一点。
  • libstdc++ 和 MSVC 应用填充和对齐。

PS:LWG 代表图书馆工作组。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-11-30
  • 2013-04-17
  • 2012-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-21
  • 2013-11-02
相关资源
最近更新 更多