小编Jay*_*Phi的帖子

非空终止char数组的std :: string_view大小的差异

我正在使用不同编译器的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)

c++ string-view c++17

1
推荐指数
1
解决办法
677
查看次数

标签 统计

c++ ×1

c++17 ×1

string-view ×1