简体   繁体   English

std :: const引用的元组?

[英]std::tuple of const references?

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. 为了清楚起见,我想将参数作为3元组传递。 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);

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. 如果它们不是多态的并且经常一起使用(我假设是创建的),那么为什么不将它们打包到值元组, std::tuple<A, A, A>或命名结构中。 You would pass it around by const reference. 您可以通过const引用传递它。 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] ? 实际上,由于所有三个元素都是同一类型,为什么不使用三个元素组成的数组A[3] Tuples are best for heterogenous types. 元组最适合异构类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM