gpt4 book ai didi

c++ - 使用具有默认值的引用参数是好习惯吗?

转载 作者:行者123 更新时间:2023-11-28 00:07:28 25 4
gpt4 key购买 nike

我有以下代码:

#include <string>
#include <iostream>

void f(const std::string& s = "")
{
std::cout << "\"" << s << "\"" << std::endl;
}

int main()
{
std::string s1 = "qwe";
f();
f("asd");
f(s1);
}

使用临时参数和不使用参数的调用有多糟糕(如果有的话)?

据我所知,这之所以能编译,是因为 const 引用延长了临时对象的生命周期,直到方法结束 http://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/

尝试在 s 参数旁边没有 const 的情况下编译相同的示例失败。

#include <string>
#include <iostream>

void f(std::string& s = "")
{
std::cout << "\"" << s << "\"" << std::endl;
}

int main()
{
std::string s1 = "qwe";
f();
f("asd");
f(s1);
}

编译

g++-5 -O3 -Wall --std=c++11 main.cpp  && ./a.out
main.cpp:4:27: error: invalid initialization of non-const reference of type ‘std::string& {aka std::basic_string<char>&}’ from an rvalue of type ‘std::string {aka std::basic_string<char>}’
void f(std::string& s = "")
^
In file included from /usr/include/c++/5/string:52:0,
from main.cpp:1:
/usr/include/c++/5/bits/basic_string.h:2893:7: note: after user-defined conversion: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
^
main.cpp: In function ‘int main()’:
main.cpp:12:5: error: invalid initialization of non-const reference of type ‘std::string& {aka std::basic_string<char>&}’ from an rvalue of type ‘std::string {aka std::basic_string<char>}’
f();
^
In file included from /usr/include/c++/5/string:52:0,
from main.cpp:1:
/usr/include/c++/5/bits/basic_string.h:2893:7: note: after user-defined conversion: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
^
main.cpp:4:6: note: in passing argument 1 of ‘void f(std::string&)’
void f(std::string& s = "")
^
main.cpp:13:10: error: invalid initialization of non-const reference of type ‘std::string& {aka std::basic_string<char>&}’ from an rvalue of type ‘std::string {aka std::basic_string<char>}’
f("asd");
^
In file included from /usr/include/c++/5/string:52:0,
from main.cpp:1:
/usr/include/c++/5/bits/basic_string.h:2893:7: note: after user-defined conversion: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
^
main.cpp:4:6: note: initializing argument 1 of ‘void f(std::string&)’
void f(std::string& s = "")

最佳答案

这不是一个糟糕的做法,但通常提供重载会更好:

void f(std::string const& s) { std::cout << "\\" << s << "\\\n"; }

void f() { f(""); }

它避免了一些最终会让很多人感到困惑的语言特性。例如,这会打印什么?

struct base { virtual void f(int i = 42) { std::cout << i; } };

struct derived : base { void f(int i = 19) { std::cout << i; }};

int main() { base * b = new derived(); b->f(); }

当您使用默认参数时也会出现歧义错误,而当您使用重载时则不会。

就特别是 const 引用而言,这并不重要。默认值在函数调用的生命周期内绑定(bind)到引用。真的一点作用都没有。有时,当编译器可以执行某些使用引用参数无法实现的优化时,您可能使用值获得更好的结果,但通常不需要担心。

当然,这不适用于非常量引用,因为它们不绑定(bind)到临时对象。

关于c++ - 使用具有默认值的引用参数是好习惯吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34728192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com