我正在翻阅C++ 0x.当我看着元组时,我看到了这个例子.我为什么要这么做get<3>(var)?为什么我不能做的var.get(index)还是var.get<index>()?我更喜欢这些使代码看起来和感觉一致.
typedef tuple< int, double, long &, const char * > test_tuple ;
long lengthy = 12 ;
test_tuple proof( 18, 6.5, lengthy, "Ciao!" ) ;
lengthy = get<0>(proof) ; // Assign to 'lengthy' the value 18.
get<3>(proof) = " Beautiful!" ; // Modify the tuple’s fourth element.
Run Code Online (Sandbox Code Playgroud)
您必须使用,get<0>因为元组的每个成员都有不同的类型.所以结果类型get<0>是int,get<1>是double,get<2>是long&等打电话时你不能做到这一点get(0),因为它必须有一个固定的返回类型.
您可能还想查看模板元编程,因为此模式是整个编程部分的基本模式之一.
http://en.wikipedia.org/wiki/Template_metaprogramming
http://www.boost.org/doc/libs/1_43_0/libs/mpl/doc/index.html
| 归档时间: |
|
| 查看次数: |
118 次 |
| 最近记录: |