c++ - 为什么 std::boolalpha 在使用 clang 时忽略字段宽度?

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

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

#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)

最佳答案

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

No provision for fill-padding when boolalpha is set

N4582 subclause 25.4.2.2.2 [facet.num.put.virtuals] paragraph 6 makes no provision for fill-padding in its specification of the behaviour when (str.flags() & ios_base::boolalpha) != 0.

因此,我没有看到使用 Clang 解决此问题的方法。但是,请注意:

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

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

关于c++ - 为什么 std::boolalpha 在使用 clang 时忽略字段宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46337810/

相关文章:

c++ - 修复 C++ 中奇怪的写入文件错误

regex - 如何编写正则表达式来匹配字符串中最大的 0 序列?

c++ - 包含头文件时出错

c++ - 循环范围内 std::valarray 的无效范围表达式

C,通过标记字符串填充二维数组

c++ - 如何在DolphinDB C++ API中转换时间类型字段的字符串格式

c++ - gluUnproject 模型 View 矩阵

c++ - 在c++中找到用户输入的3个最大数字的平均值

python - 为什么这个文件不会写入文件系统

Java:使用文本文件