温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++中什么时候传递const参照

发布时间:2021-11-26 15:58:56 来源:亿速云 阅读:99 作者:iii 栏目:大数据

本篇内容介绍了“C++中什么时候传递const参照”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

对于输入参数来说,拷贝代价小的传值,其他传递const参照

Reason(原因)

两种方式都可以让调用者知道函数不会修改参数并且都可以通过右值初始化。

什么是“拷贝代价小”和机器架构有关,但是2到3个字(双精度数,指针,引用)通常最适合传值。如果拷贝代价小,没有方法可以超过拷贝的简单和安全,另外,对于小对象(不超过2到3个字)来说,由于函数不需要额外间接访问,因此传值会比传址的速度更快。

Example(示例)

void f1(const string& s);  // OK: pass by reference to const; always cheap
void f2(string s);         // bad: potentially expensive
void f3(int x);            // OK: Unbeatable
void f4(const int& x);     // bad: overhead on access in f4()

(只)对于高级的用法,需要优化为向输入参数传递右值引用的情况有:

  • If the function is going to unconditionally move from the argument, take it by &&. See F.18.

    如果函数会无条件的移动参数的内容,使用&&。参考F.18

  • If the function is going to keep a copy of the argument, in addition to passing by const& (for lvalues), add an overload that passes the parameter by && (for rvalues) and in the body std::moves it to its destination. Essentially this overloads a "will-move-from"; see F.18.

    如果函数会管理一个参数的拷贝,除了使用功能const&(对于左值)以外,增加一个使用&&(对于右值)传递参数的重载函数并且在内部使用std::move移动参数内容到目标上。本质上这个重载是一个“将要移动形式”;参考F.18

  • In special cases, such as multiple "input + copy" parameters, consider using perfect forwarding. See F.19.

    对于

  • 特殊场合,例如多重“输入+拷贝”参数,考虑使用完美的forward。

Example(示例)

int multiply(int, int); // just input ints, pass by value
// suffix is input-only but not as cheap as an int, pass by const&string& concatenate(string&, const string& suffix);
void sink(unique_ptr<widget>);  // input only, and moves ownership of the widget

避免使用“只有高手才懂的技术”,例如:

  • Passing arguments as T&& "for efficiency". Most rumors about performance advantages from passing by && are false or brittle (but see F.18 and F.19).

    为了提高效率而使用T&&。许多通过传递&&获得性能优势的传言都是假的或者脆弱的。

  • Returning const T& from assignments and similar operations 

    通过复制或者类似操作返回const T&

Example(示例)

假设Matrix实现了移动操作(例如使用std::vector保管元素)

Matrix operator+(const Matrix& a, const Matrix& b){    Matrix res;    // ... fill res with the sum ...    return res;}
Matrix x = m1 + m2;  // move constructor
y = m3 + m3;         // move assignment
Notes(注意)

返回值优化不会处理赋值的情况,但是移动赋值会。

可以假设引用参照的是有效对象(语言准则)。不存在(合理的)“空引用”。如果需要可选值概念,使用指针,std::optional或者特殊值表示“没有值”。

Enforcement(实施建议)
  • (Simple) ((Foundation)) Warn when a parameter being passed by value has a size greater than 2 * sizeof(void*). Suggest using a reference to const instead.

    (简单)((基本准则)) 当传值的大小超过2*sizeof(void*)时,报警。建议使用const引用。

  • (Simple) ((Foundation)) Warn when a parameter passed by reference to const has a size less than 2 * sizeof(void*). Suggest passing by value instead.

    (简单)((基本准则))当小于2*sizeof(void*)的参数使用const传址时,报警。

  • (Simple) ((Foundation)) Warn when a parameter passed by reference to const is moved.

    (简单)((基本准则))当使用const传址的参数的内容被移动时,报警。

“C++中什么时候传递const参照”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI