void Foo1(string_view view) {
...
}
string str = "one two three";
Foo1("one two three"); // Implicitly convert char* to string_view
Foo1(str);
我想知道哪个构造函数将 char* 隐式转换为 string_view,哪个构造函数将字符串隐式转换为 string_view?
我知道构造函数 (4) 将 const char* 转换为 string_view 但我传递的是 char*。
最佳答案
std::string_view
有一个非 explicit
converting constructor服用 const char*
, 支持从 const char*
的隐式转换至 std::string_view
.
constexpr basic_string_view(const CharT* s);
当你说:
but what I passed is
char*
.
您实际上是在传递一个字符串文字(即
"one two three"
),其类型为 const char[]
,衰减到 const char*
.std::string
有一个非 explicit
conversion operator支持从 std::string
的隐式转换至 std::string_view
.
constexpr operator std::basic_string_view<CharT, Traits>() const noexcept;
关于c++ - 将字符串隐式转换为 string_view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63166194/