- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下代码:
#include <optional>
#include <string_view>
int main() {
std::optional<std::string_view> opt { "abc" };
std::cout << opt.value();
return 0;
}
它编译并在运行时向控制台输出“abc”。为什么要编译? main中的第一行代码应该是:
std::optional<std::string_view> { std::string_view { "abc" } };
编译器如何知道用文字调用 string_view 构造函数?这似乎不仅仅是我以前见过的类型推导。在这里,编译器似乎正在向我的源代码添加代码,即调用构造函数。
最佳答案
How does the compiler know to make a call to the string_view constructor with the literal?
std::optional
有一个形式为
template < class U = T >
constexpr optional( U&& value );
仅当T
时才用于直接初始化底层对象可以从 U
构建,在这种情况下可以使用 std::string_view
的
constexpr basic_string_view(const CharT* s);
构造函数。
This seems like it is doing more than just type deduction which I have seen before
你告诉编译器你有一个 std::optional<std::string_view>
,所以它不需要确定你有什么类型的可选。它确实确定了 U
将在构造函数中,并看到这是一种可用于构造 std::string_view
的类型with,因此它调用构造函数来执行此操作,因为它满足要求。
关于c++ - C++ 如何确定这应该是一个 string_view?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68898411/
假设我们有一个 string_view 和另一个 string_view,它是第一个 string_view 的子集: using namespace std; // just to shorten
我正在使用一个使用 boost::string_view 的 boost 库。但是,我想在我的代码中使用 std::string_view。 问:在这两者之间进行转换的最佳方式是什么? 目前我正在使用
std::string_view::remove_prefix() 和 std::string_view::remove_suffix() 都是 c 中的 constexpr 成员函数++17;但是,
我已经从 Bjarne Stroustrup 的 A Tour of C++ 中复制代码来测试字符串 View ,但我不断收到错误: error: no matching function for c
除了 std::string_view 方法之外,std::string_view 是否比 char* 有任何优势? 如果没有使用 string_view 的方法,是否有任何理由将 char* 重构为
我有这个循环: for (const std::string_view resource : resources) { ... } 由此资源被定义为 inline const std::string_
我有这个循环: for (const std::string_view resource : resources) { ... } 由此资源被定义为 inline const std::string_
而一个 span可以从一个范围构建,一个 string_view不能从一系列字符构造。 因此,例如,需要以下代码: // assume chars_span is a span of chars st
void Foo1(string_view view) { ... } string str = "one two three"; Foo1("one two three"); // Implicit
我有一个字符串,其中包含用 , 字符分隔的数字序列。为了将序列中的值读入数组,我创建了以下 GCC 10 拒绝编译的代码: #include #include #include #include
我在使用 Boost 1.70 时发现了一个相当令人费解的问题,有时 boost::string_view 似乎指向另一个字符串。 这是创建 boost::string_view 的函数调用,如下所示
在以下场景中使用 std::string_view: struct A : public std::exception{ A (const char* c) : v_(c){} con
我正在尝试尽可能多地使用 std::string_view 来包装 C 字符串,但是,每当我正在包装的 C 字符串被动态分配时,我都依赖于此图案: char *cs = get_c_string();
我读了The most elegant way to iterate the words of a string并享受答案的简洁性。现在我想对 string_view 做同样的事情。问题是,strin
我有以下代码: #include class Foo { public: Foo(std::string_view) {} }; 当我这样做时,一切都可以正常编译(使用 clang v8,C
根据一篇文章(here 和 there),这段代码是一个错误的免费示例: #include #include #include int main() { std::string s = "H
string_view 是 C++ 库基础 TS(N3921) 中的一项提议功能,添加到 C++17 据我所知,它是一种代表某种字符串“概念”的类型,它是任何类型的容器的 View ,可以将可视内容存
考虑以下代码: #include #include int main() { std::optional opt { "abc" }; std::cout { std::stri
在 std::string_view 上匹配正则表达式工作正常。但是当我返回匹配的子字符串时,它们会因为某种原因而消失。 std::string_view 参数在函数作用域结束时被销毁,但它指向的内存
This reference指出第二个示例产生了一个悬空指针。如何在第二个表达式中创建悬挂指针而不是在第一个表达式中? std::string_view good("a string literal"
我是一名优秀的程序员,十分优秀!