std::const 引用的元组?
我有一个函数,以前被称为 为
void func(const A& v0, const A& v1, const A& v2);
清楚起见,我想将参数作为 3 元组传递。如果要避免任何额外的开销,正确的方法是什么?以下是最有效的吗?:
void func(std::tuple<const A&> v);
I had a function which used to be called as
void func(const A& v0, const A& v1, const A& v2);
For clarity, I would like to pass the arguments as a 3-tuple. What is the right way if any additional overhead is to be avoided? Is the following going to be the most efficient?:
void func(std::tuple<const A&> v);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果它们不是多态的并且经常一起使用(并且我假设是创建的),那么为什么不将它们打包到一个值元组中,
std::tuple
,或命名结构。您可以通过 const 引用传递它。该解决方案没有性能开销,而且实际上可能更快。实际上,由于所有三个元素都是同一类型,为什么不使用三个元素的数组
A[3]
呢?元组最适合异构类型。If they are not polymorphic and often used (and, I assume, created) together then why don't you just pack them into a value tuple,
std::tuple<A, A, A>
, or a named struct. You would pass it around by const reference. This solution has no performance overhead and, in fact, may be faster.Actually, since all three elements are of the same type, why don't you use an array of three elements,
A[3]
? Tuples are best for heterogenous types.