我找到了一个模糊的日志记录错误,发现长度为2的初始化列表似乎是特例!这怎么可能?
该代码是使用Apple LLVM 5.1版(clang-503.0.40)编译的CXXFLAGS=-std=c++11 -stdlib=libc++
。
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef vector<string> Strings;
void print(string const& s) {
printf(s.c_str());
printf("\n");
}
void print(Strings const& ss, string const& name) {
print("Test " + name);
print("Number of strings: " + to_string(ss.size()));
for (auto& s: ss) {
auto t = "length = " + to_string(s.size()) + ": " + s;
print(t);
}
print("\n");
}
void test() {
Strings a{{"hello"}}; print(a, "a");
Strings b{{"hello", "there"}}; print(b, "b");
Strings c{{"hello", "there", "kids"}}; print(c, "c");
Strings A{"hello"}; print(A, "A");
Strings B{"hello", "there"}; print(B, "B");
Strings C{"hello", "there", "kids"}; print(C, "C");
}
int main() {
test();
}
输出:
Test a
Number of strings: 1
length = 5: hello
Test b
Number of strings: 1
length = 8: hello
Test c
Number of strings: 3
length = 5: hello
length = 5: there
length = 4: kids
Test A
Number of strings: 1
length = 5: hello
Test B
Number of strings: 2
length = 5: hello
length = 5: there
Test C
Number of strings: 3
length = 5: hello
length = 5: there
length = 4: kids
我还应该补充一点,测试b中的假字符串的长度似乎不确定-它总是大于第一个初始化程序的字符串,但是从大于第一个字符串的长度到两个字符串的总长度之间变化在初始化程序中。