使用带有默认值的参考参数是否很好?

时间:2016-01-11 18:07:44

标签: c++ reference pass-by-reference temporary const-reference

我有以下代码:

#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/

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

#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 = "")

2 个答案:

答案 0 :(得分:2)

这不是一种可怕的做法,但通常 更好地提供重载:

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引用而言,这并不重要。默认值绑定到函数调用生命周期的引用。它根本没有任何影响。当编译器可以执行某些参考参数无法实现的优化时,可能使用值获得更好的结果,但通常不需要关注。

当然,这不适用于非const引用,因为它们不会绑定到临时引用。

答案 1 :(得分:1)

默认值是允许您在没有任何参数的情况下调用函数,因此如果您有时不将参数传递给函数,请使用带默认值的reference参数,否则无论如何都要这样做。