【问题标题】:Is using a reference parameter with default value good practice?使用具有默认值的参考参数是好的做法吗?
【发布时间】:2016-01-11 18:07:44
【问题描述】:

我有以下代码:

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

【问题讨论】:

  • 尝试在 s 参数旁边没有 const 的情况下编译相同的示例失败请发布该代码。
  • 不要使用std::endl,除非你需要它做的所有额外的东西。 '\n' 开始新的一行。
  • 这与延长寿命没有任何关系。临时对象一直存在到完整表达式结束,这就是函数调用所需要的全部内容。
  • @Pete Becker 你能详细说明一下吗?
  • 了解std::endl 的作用。然后决定是否需要它。提示:你没有。

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


【解决方案1】:

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

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 引用而言,这并不重要。默认值在函数调用的生命周期内绑定到引用。真的一点效果都没有。当编译器可以执行某些使用引用参数无法实现的优化时,您可能有时使用值获得更好的结果,但通常不需要担心。

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

【讨论】:

  • @old_mountain: struct 使用public: 作为默认可见性,所以derived : base 就足够了。
【解决方案2】:

默认值是为了让你可以不带任何参数地调用函数,所以如果你有时不给函数传参,就使用带默认值的引用参数,否则没关系。

【讨论】:

    猜你喜欢
    • 2013-04-25
    • 2019-09-11
    • 2012-12-15
    • 1970-01-01
    • 2016-10-30
    • 2017-05-11
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多