std::string 引用类成员的奇怪行为
作者:morpheus 提问时间:7/29/2023
给定以下代码: #include <iostream> class Foo { public: Foo(const std::string& label) : label_(label) {}...
const-reference 问答列表
作者:morpheus 提问时间:7/29/2023
给定以下代码: #include <iostream> class Foo { public: Foo(const std::string& label) : label_(label) {}...
作者:Marco E 提问时间:2/2/2020
闭。这个问题是不可重现的,或者是由错别字引起的。它目前不接受答案。 这个问题是由一个错别字或一个无法再重现的问题引起的。虽然类似的问题可能在这里成为主题,但这个问题的解决方式不太可能帮助未来的读者。...
作者:Dávid Tóth 提问时间:11/4/2019
在具有常量引用作为成员的类中使用 copy-and-swap 习语时, 出现上述错误。 示例代码: #include <iostream> #include <functional> using...
作者:111111 提问时间:11/10/2020
闭。此问题需要调试详细信息。它目前不接受答案。 编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将有助于其他人回答这个问题。 3年前关闭。 改进此问题 我有以下功能: ...
作者:Артём Гаркавый 提问时间:2/23/2021
我有两个功能 void f(const int &x) {} void g(int& x) {} 我能做到 int x = 0; std::thread t1(f, x); 但是我不能创造...
作者:Alex O 提问时间:10/28/2021
我经常遇到按值传递和移动的习语: struct Test { Test(std::string str_) : str{std::move(str_)} {} std::string str; ...
作者:simplekind 提问时间:6/30/2021
这个问题在这里已经有答案了: 对象、右值引用、常量引用之间的重载解析 (1 个答案) 去年关闭。 所以我用 C++ 11 写了一段代码 #include <iostream> using name...
作者:AssistantToTheRegionalManager 提问时间:7/2/2022
这个问题在这里已经有答案了: 为什么要使用 const 成员函数? (3 个答案) 算子重载的基本规则和习语是什么? (10 个回答) 去年关闭。 我一直在研究一个矩阵类,最近我学会了如何将常量引用...
作者:Enlico 提问时间:7/14/2022
由于引用 () 的行为为常量指针 (),因此通过“对指针的常量引用”参数 () 传递参数似乎有点多余,因为some-type&some-type * constsome-type * const& ...
作者:Enlico 提问时间:1/26/2023
我刚刚看到了这样的代码 /* whatever */ foo(std::string const& s) { // stuff auto L = s.length(); int i{/* in...