c++ - 我可以让 C++ 编译器决定是按值传递还是按引用传递?

标签 c++ performance compiler-construction pass-by-value pass-by-const-reference

看看这个假设的头文件:

template <class T>
class HungryHippo {
public:
    void ingest(const T& object);
private:
    ...
}

现在,对于 HungryHippo<string>你想要 ingest 是有道理的对字符串的引用——复制一个字符串可能非常昂贵!但是对于 HungryHippo<int>它变得不那么有意义了。通过 int直接可以非常便宜(大多数编译器会在寄存器中执行),但是传递对 int 的引用是额外的不必要的间接级别。这也适用于返回值。

有没有办法向编译器建议“嘿,我不会修改参数,所以决定是通过值传递还是通过引用传递,这取决于你的想法更好”?

一些可能相关的事情:

  • 我可以通过编写 template <class T, bool PassByValue> class HungryHippo 手动伪造此效果然后专攻PassByValue .如果我想变得很花哨,我什至可以推断 PassByValue基于 sizeof(T)std::is_trivially_copyable<T> .无论哪种方式,当实现看起来几乎相同时,这都是很多额外的工作,我怀疑编译器可以比我更好地决定是否按值传递。
  • libc++项目似乎通过内联许多函数来解决这个问题,因此编译器可以做出上一级的选择,但在这种情况下,假设 ingest 的实现相当复杂,不值得内联。 正如评论中所解释的,所有模板函数都是 inline默认情况下。

最佳答案

boost::call_traits header 正是处理这个问题。看看here .

具体来说,call_traits<T>::param_type选项包括以下描述:

If T is a small built in type or a pointer, then param_type is defined as T const, instead of T const&. This can improve the ability of the compiler to optimize loops in the body of the function if they depend upon the passed parameter, the semantics of the passed parameter is otherwise unchanged (requires partial specialization).

在您的情况下,您可以定义 ingest如下:

template <class T>
class HungryHippo {
public:
    void ingest(call_traits<T>::param_type object);
    // "object" will be passed-by-value for small 
    // built-in types, but passed as a const reference 
    // otherwise
private:
    ...
};

这是否真的会对您的实际代码/编译器组合产生很大影响,我不确定。与往常一样,您必须运行一些实际的基准测试,看看会发生什么......

关于c++ - 我可以让 C++ 编译器决定是按值传递还是按引用传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13408423/

相关文章:

c++ - 如何使用 boost::process 向子进程发送 SIGTERM

C++ 从调用指定函数/方法的位置进行检查

c++ - 返回 QList 的函数

performance - 是否有一个普遍可接受的(软)实时延迟定义?

c++ O3优化在循环时中断工作

performance - 如何删除 quilljs 中未使用的模块

Android Studio 太慢和滞后

ruby - 在编译过程中何时/何地发生类型检查

c++ - 编译器会优化 malloc/free 或 new/delete 对到 alloca

c - 汇编、机器码、字节码和操作码之间的实际关系是什么?