资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

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

创新互联是一家专注于成都网站设计、网站建设与策划设计,江岸网站建设哪家好?创新互联做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:江岸等地区。江岸做网站价格咨询:18980820575

对于输入参数来说,拷贝代价小的传值,其他传递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);  // 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参照”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


分享题目:C++中什么时候传递const参照
分享链接:http://cdkjz.cn/article/goeodj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220