我正在使用不同编译器的std :: string_view,并注意到每个编译器在使用非null终止的char数组初始化std :: string_view时会打印出不同的大小.
似乎每个编译器在打开优化时打印出正确的大小,但在优化关闭时打印出错误的大小(GCC除外,它在两种情况下都打印出正确的大小).
我的问题是:为什么会这样?
码:
// test.cpp
#include <iostream>
#ifdef __MINGW32__
#include <experimental/string_view>
#elif _MSC_VER
#include <string_view>
#endif
int main()
{
const char foo[3]{ 'f','o','o' };
#ifdef __MINGW32__
std::experimental::string_view str_v{ foo };
#elif _MSC_VER
std::string_view str_v{ foo };
#endif
std::cout << sizeof(foo) << " " << str_v.size() << '\n';
}
Run Code Online (Sandbox Code Playgroud)
输出:Visual C++ 19.00.24619.0
3 5 // cl /Zi /std:c++latest /EHsc /nologo /W4 test.cpp
3 3 // cl /O2 /std:c++latest /EHsc /nologo /W4 test.cpp …Run Code Online (Sandbox Code Playgroud)