在C++中函数只能返回一个值,std标准库中也没有能很好满足tuple功能的类实现,boost::tuple 则填补了这一空缺。
元素
目前版本的boost::tuple支持0~10元素,元素类型可以是任何C++的类型。
1
tuple
<
int
>
2 tuple < double & , const double & , const double , double * , const double *>
3 tuple < A, int ( * )( char , int ), B(A:: * )(C & ), C >
4 tuple < std:: string , std::pair < A, B > >
5 tuple < A * , tuple < const A * , const B & , C > , bool , void *>
2 tuple < double & , const double & , const double , double * , const double *>
3 tuple < A, int ( * )( char , int ), B(A:: * )(C & ), C >
4 tuple < std:: string , std::pair < A, B > >
5 tuple < A * , tuple < const A * , const B & , C > , bool , void *>
构造tuple
构造tuple时如果没有提供足够多的参数来初始化元素,那么将使用缺省值进行初始化,但是元素类型必须要能支持缺省值初始化。
1
tuple
<
double
&>
()
//
错误!引用类型必须显示初始化
2
3 double d = 5 ;
4 tuple < double &> (d) // ok
5
6 tuple < double &> (d + 3.14 ) // 错误!不能使用临时数据对象来初始化non-const reference
7
8 tuple < const double &> (d + 3.14 ) // ok, but dangerous: 元素成为空悬引用(dangling reference),指向一个不存在的变量值
2
3 double d = 5 ;
4 tuple < double &> (d) // ok
5
6 tuple < double &> (d + 3.14 ) // 错误!不能使用临时数据对象来初始化non-const reference
7
8 tuple < const double &> (d + 3.14 ) // ok, but dangerous: 元素成为空悬引用(dangling reference),指向一个不存在的变量值
boost::make_tuple函数
使用boost::make_tuple函数创建tuple可以省去列举模版元素类型的繁琐,make_tuple缺省创建的元素为非引用类型,使用boost::ref和boost::cref可以创建引用类型元素:
1
A a;
2 B b;
3 const A ca = a;
4 make_tuple(cref(a), b); // creates tuple<const A&, B>
5 make_tuple( ref (a), b); // creates tuple<A&, B>
6 make_tuple( ref (a), cref(b)); // creates tuple<A&, const B&>
7 make_tuple(cref(ca)); // creates tuple<const A&>
8 make_tuple( ref (ca)); // creates tuple<const A&>
2 B b;
3 const A ca = a;
4 make_tuple(cref(a), b); // creates tuple<const A&, B>
5 make_tuple( ref (a), b); // creates tuple<A&, B>
6 make_tuple( ref (a), cref(b)); // creates tuple<A&, const B&>
7 make_tuple(cref(ca)); // creates tuple<const A&>
8 make_tuple( ref (ca)); // creates tuple<const A&>
需要注意的是字符常量的类型为const character array,而不是const char *。
1
make_tuple(
"
Donald
"
,
"
Daisy
"
);
//
creates tuple<const char (&)[7], const char (&)[6]>
访问tuple元素
tuple元素使用下面的方法进行访问:
t.get<N>()
或者
boost::get<N>(t)
boost::get<N>(t)
其中t为tuple对象,N为元素序号,返回值为引用类型,如果t为const对象则返回const reference。
1
double
d
=
2.7
;
2 A a;
3 tuple < int , double & , const A &> t( 1 , d, a);
4
5 const tuple < int , double & , const A &> ct = t;
6
7 int i = get < 0 > (t);
8 i = t. get < 0 > (); // ok
9
10 int j = get < 0 > (ct); // ok
11 get < 0 > (t) = 5 ; // ok
12 get < 0 > (ct) = 5 ; // error, can't assign to const
13
14 double e = get < 1 > (t); // ok
15 get < 1 > (t) = 3.14 ; // ok
16 get < 2 > (t) = A(); // error, can't assign to const
17
18 A aa = get < 3 > (t); // error: index out of bounds
19
20 ++ get < 0 > (t); // ok, can be used as any variable
2 A a;
3 tuple < int , double & , const A &> t( 1 , d, a);
4
5 const tuple < int , double & , const A &> ct = t;
6
7 int i = get < 0 > (t);
8 i = t. get < 0 > (); // ok
9
10 int j = get < 0 > (ct); // ok
11 get < 0 > (t) = 5 ; // ok
12 get < 0 > (ct) = 5 ; // error, can't assign to const
13
14 double e = get < 1 > (t); // ok
15 get < 1 > (t) = 3.14 ; // ok
16 get < 2 > (t) = A(); // error, can't assign to const
17
18 A aa = get < 3 > (t); // error: index out of bounds
19
20 ++ get < 0 > (t); // ok, can be used as any variable
Tiers类型
Tiers是一种特殊的tuple类型,它的所有元素都是 non-const reference 类型,通过函数模版tie创建:
1
int
i;
char
c;
double
d;
2 tie(i, c, a); // create tuple<int&, char&, double&>
2 tie(i, c, a); // create tuple<int&, char&, double&>
使用tie函数可以将tuple解包('unpack')成一组独立的变量:
1
int
i;
char
c;
double
d;
2 tie(i, c, d) = make_tuple( 1 , ' a ' , 5.5 );
3 std::cout << i << " " << c << " " << d;
2 tie(i, c, d) = make_tuple( 1 , ' a ' , 5.5 );
3 std::cout << i << " " << c << " " << d;
Ignore对象
ignore对象用来忽略无用的tuple元素,ignore位于boost::tuples名字空间下。
1
char
c;
2 tie(tuples::ignore, c) = std::make_pair( 1 , ' a ' );
2 tie(tuples::ignore, c) = std::make_pair( 1 , ' a ' );