通过引用比通过价值更贵 [英] Pass by reference more expensive than pass by value

查看:98
本文介绍了通过引用比通过价值更贵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种情况,其中pass-by-reference比C ++中的pass-by-value更昂贵?

Is there a case where pass-by-reference is more expensive than pass-by-value in C++? If so, what would that case be?

推荐答案

希望传递基本类型(int,char,float,...)

Prefer passing primitive types (int, char, float, ...) and POD structs that are cheap to copy (Point, complex) by value.

这将比通过引用传递时所需的间接效率更高。

This will be more efficient than the indirection required when passing by reference.

请参阅 Boost的通话特征。 p>

See Boost's Call Traits.


模板类 call_traits< T> 封装了传递参数的best的某个类型T到或来自一个函数,并且由如下表中定义的typedef的集合组成。 call_traits 的目的是确保不会发生像引用引用这样的问题,并且尽可能以最有效的方式传递参数。

The template class call_traits<T> encapsulates the "best" method to pass a parameter of some type T to or from a function, and consists of a collection of typedefs defined as in the table below. The purpose of call_traits is to ensure that problems like "references to references" never occur, and that parameters are passed in the most efficient manner possible.

这篇关于通过引用比通过价值更贵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆