【发布时间】: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