【发布时间】:2020-09-17 09:15:59
【问题描述】:
很明显std::array<type,count> 不能存储引用。但是可以写
std::tuple<int &, int &, int &, int &, int &>
得到你所期望的。见
#include <array>
#include <tuple>
#include <iostream>
int main(){
int a = 10;
int b = 20;
int c = 20;
// Does not compile
//std::array<int&,3> x = {a,b,c};
using TInt3 = std::tuple<int&,int&,int&>;
TInt3 y = {a,b,c};
std::cout << sizeof(TInt3) << std::endl;
std::get<0>(y)=11;
std::cout << "a " << a << std::endl;
}
a 11 在哪里输出。然而,长手写出来是很乏味的。如何生成类型(在 c++11 中)
TupleAllSame<type, count>
所以下面是等价的
TupleAllSame<int &, 2> <--> std::tuple<int &, int &>
TupleAllSame<double, 4> <--> std::tuple<double, double, double, double>
TupleAllSame<std::string &, 3> <--> std::tuple<std::string &, std::string &, std::string &>
【问题讨论】:
标签: c++ c++11 tuples template-meta-programming