看看这个假设的头文件:
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, thenparam_type
is defined asT const
, instead ofT 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/