初始化包含元组数组的 class 的正确且简单的方法是什么?
what is the correct and easy way for initialization of class containing array of tuples?
我有一个 class,其中包含如下所示的元组数组:
template<size_t __v, typename ... __tz>
class turray{
public:
std::tuple<__tz ...> array[__v];
};
它没有任何用户定义的构造函数,我想知道如何初始化它。
请考虑以下方法:
int main(){
turray<2, int, float> mturray0{std::tuple<int, float>{1, 1.1}, std::tuple<int, float>{2, 2.2}}; //works but is very big
turray<2, int, float> mturray1{{1, 1.1}, {2, 2.2}};// causes error
}
第一种方法可行,但体积太大,并不理想。第二种方法导致以下错误:
error: too many initializers for ‘turray<2, int, float>’
227 | turray<2, int, float> mturray1{{1, 1.1}, {2, 2.2}};
| ^
如果有人能告诉我什么是正确的方法,我将不胜感激。
你只需要再加一对牙套:
turray<2, int, float> mturray1 { { {1, 1.1}, {2, 2.2} } };
// ^ ^ tuple
// ^ ^ array
// ^ ^ turray
这是一个demo。
我有一个 class,其中包含如下所示的元组数组:
template<size_t __v, typename ... __tz>
class turray{
public:
std::tuple<__tz ...> array[__v];
};
它没有任何用户定义的构造函数,我想知道如何初始化它。 请考虑以下方法:
int main(){
turray<2, int, float> mturray0{std::tuple<int, float>{1, 1.1}, std::tuple<int, float>{2, 2.2}}; //works but is very big
turray<2, int, float> mturray1{{1, 1.1}, {2, 2.2}};// causes error
}
第一种方法可行,但体积太大,并不理想。第二种方法导致以下错误:
error: too many initializers for ‘turray<2, int, float>’
227 | turray<2, int, float> mturray1{{1, 1.1}, {2, 2.2}};
| ^
如果有人能告诉我什么是正确的方法,我将不胜感激。
你只需要再加一对牙套:
turray<2, int, float> mturray1 { { {1, 1.1}, {2, 2.2} } };
// ^ ^ tuple
// ^ ^ array
// ^ ^ turray
这是一个demo。