如果我返回文字而不是声明的 std::string 会发生什么
What happens if I return literal instead of declared std::string?
假设我们有一个实用程序函数:
std::string GetDescription() { return "The description."; }
可以返回字符串文字吗?是否复制了隐式创建的std::string
对象?
我想过总是这样返回它:
std::string GetDescription() { return std::move(std::string("The description.")); }
但它当然更长,更冗长。我们也可以假设编译器 RVO 会对我们有所帮助
std::string GetDescription() { return std::string("The description."); }
然而,我仍然不知道它到底要做什么,而不是它能做什么。
std::string GetDescription() { return "XYZ"; }
相当于这个:
std::string GetDescription() { return std::string("XYZ"); }
这反过来又相当于:
std::string GetDescription() { return std::move(std::string("XYZ")); }
意味着当您返回std::string("XYZ")
哪个临时对象时,std::move
是不必要的,因为无论如何都会移动该对象(隐式)。
同样,当您返回 "XYZ"
时,显式构造std::string("XYZ")
是不必要的,因为构造无论如何都会(隐式)发生。
所以这个问题的答案是:
是否复制了隐式创建的 std::string 对象?
是否。隐式创建的对象毕竟是(隐式)移动的临时对象。但是编译器可以省略这一举动!
所以底线是这样的:你可以写这个代码并感到高兴:
std::string GetDescription() { return "XYZ"; }
在某些极端情况下,return tempObj
比return std::move(tempObj)
更有效(因此更好)。
可以返回字符串文字吗?是否复制了隐式创建的 std::string 对象?
还行。你得到的是 std::string 的(隐式)构造函数,创建一个本地副本,然后作为右值引用返回。将客户端代码中的结果转换为字符串,将从右值引用设置该字符串。
如果你使用第二段代码,你就"说得太多了"。代码是正确的,并且它们(几乎)等效(它们应该是等效的,但允许编译器在第一种情况下执行的优化更好*)。
我会选择:
std::string GetDescription() { return std::string("The description."); }
这样,你返回一个字符串是明确的,并且代码(几乎)是最小的:你依赖于std::string move-construction。
*) 根据@SteveJessop的评论进行了相应编辑。
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 为std::string的某个索引赋值
- std中有类似find_last_of的函数,而string中没有
- 使用 std::string () const 函数启动线程或未来
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 如何更改大小(std::string)
- std::string 的对象真的可以移动吗?
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 无法从 std::string 中提取C++ Unicode 符号
- std::string 构造函数如何处理固定大小的 char[]?
- 确切地说,如何解释 std::getline(stream, string) 函数在C++中填充的字符串
- C++:考虑C++ std::string 中双引号 (") 的字面含义,而不使用反斜杠 (\)
- 'int main(int, int, std::__cxx11::string, std::__cxx11::string)'只需要零或两个参数 [-Wmain]
- C++ 在列表和列表之间选择返回类型<<string>std::p air<string,string>>
- std::string, std::wstring and UTF8
- uan inplace replacement of std::string/std::wstring?
- #define Glib::string std:wstring
- 从 std::string & std::wstring 中获取 char 整数值
- BSTR转换为std::string(std::wstring),反之亦然