Tuples
Herb Sutter(陶章志译)
就像我上期所报道的一样,在2002十月标准会议上,两个库扩展作为标准库延深,而被通过。
1 是Doug Gregor’s提出的多态函数的object wrappers。
2 Jaakko Järvi's提出的tuple类型。
这两个都是直接来在Boost项目。(Boost项目是一个C++ libraries 集合)上次,我答应在这期和下一期将介绍这两个扩展的库,这个月,就让我来简单的介绍一下tuple类型。
Tuple Types:一个简单Motivating例子
假如你想用一个函数返回多于一个返回值,例如: // yields a quotient only // int IntegerDivide( int n, int d ) { return n / d; } // Sample use: cout << "quotient = " << IntegerDivide( 5, 4 ); 在这个实现中有什么错误吗? 也许没有,毕竟在编译器中,我们内嵌了整数除法。包括结果也能够四舍五入。 但是,如果我们想做更多。特别,想提供一个方法得到除法其他的信息,例如除法的余数。如果没有改变函数的结构。那么,实现这样的要求的函数不是一件容易的事情。 一种办法我们在函数中加入一个输出变量。 // Example 1(b): Integer division, // yielding a quotient and remainder, // one as the return value and one via // an output parameter // int IntegerDivide( int n, int d, int& r ) { r = n % d; return n / d; } // Sample use: int remainder; int quotient = IntegerDivide( 5, 4, remainder ); cout << "quotient = " << quotient << "remainder = " << remainder; 这个方法的实现比较,但是我们经常这么实现。这种通过返回值和输出变量来返回函数返回值的办法,看起来有点不可思议。有人也许会说下面的办法更好。 // Example 1(c): Integer division, // yielding a quotient and remainder, // this time via two output parameters // void IntegerDivide( int n, int d, int& q, int& r ) { r = n % d; q = n / d; } // Sample use: int quotient, remainder; IntegerDivide( 5, 4, quotient, remainder ); cout << "quotient = " << quotient << "remainder = " << remainder; 这种办法也许更加协调。但是 还是比较含糊,不令人满意。稍微想一想,我们会记得为什么:Ralph Waldo Emerson建议我们:“一个愚笨的一致性的想法是思想混乱的怪物”(a foolish consistency is the hobgoblin of little minds)。这个版本能够正常工作,但是,如果你认为它不稳定的话,我不会责怪你。 那么该怎么做呢?在这一点我们通常会想起在标准库中我们有一个工具:std::pair,毕竟在标准模板库中有很多函数可以返回几个值 ,iterator范围就是作为一个单独的值-同时,大多通过pair<iterator,iterator>实现的,同样的方法能够运行,如下: // Example 1(d): Integer division, // yielding a quotient and remainder, // this time both in the return value // std::pair<int,int> IntegerDivide( int n, int d ) { return pair<int,int>( n/d, n%d ); } // Sample use: pair<int, int> quot_rem = IntegerDivide( 5, 4 ); cout << "quotient = " << quot_rem.first << "remainder = " << quot_rem.second; 可以看出这是一个满意的做法,同时,它还可以提高。 Tuples in Action 一些语言,包括Haskell, ML, 以及Python,都直接支持tuple types。C++不是这样,这是因为C++是一个能做任何事情,和内建标准库的系统语言,因此,我们能够,以库的形式实现我们自己的tuple types。像java等语言是把tuple type作为pair一个系列打包在一起。一个 tuple type和“bundle-o-values”很相像。 在下面一个tuple-ized 的IntegerDivide例子和上面pair-ized 是很相像的,但是,我们不要被迷惑了,毕竟它使用的是一种新的方法: // Example 2(a): Integer division, // yielding a quotient and remainder, // via a type return type // tuple<int,int> IntegerDivide( int n, int d ) { return tuple<int,int>( n/d, n%d ); } // Sample use: tuple<int, int> quot_rem = IntegerDivide( 5, 4 ); cout << "quotient = " << quot_rem.get<0>() << "remainder = " << quot_rem.get<1>(); 这个例子的语法没有pair那么优雅,但是,它却是和pairs一样的简单好用。 另一方面,typle 不局限于只有两个成员,它可以有任意多的成员,因此,它可以捆绑任何多个数值,我们来看下面的例子: // Example 2(b): Floating-point division, // yielding a quotient and remainder, // but also an underflow // tuple<float, float, bool> // quotient, remainder, underflow FloatDivide( float n, float d ) { // — } 如果,我们使用std::pair来实现的话,那么将会是这样,std::pair<float, std::pair<float, bool> >, (译注:这样大家也许能够看出tuple的优势了把) 但是,我们不能老是把tuple作为bundle-o-values来使用。这里有一些方法把来说怎样把一些独立的变量捆绑成tuple 。这是对于捆绑数值和解绑数值都是有用。例如,我们回到第一个关于除法例子 。 // Example 3: Bundling and unbundling // using "tie" // tuple<int,int> IntegerDivide( int n, int d ) { return tuple<int,int>( n/d, n%d ); } // Sample use: int quotient, remainder; tie( quotient, remainder ) = IntegerDivide( 5, 4 ); cout << "quotient = " << quotient << "remainder = " << remainder; 通过这种方法,我们就不用写那些我们不喜欢写的输出变量了,Tuples有自己的输入,输出符号,和解压操作符号。 // Example 4(a): Streaming tuples // tuple<int, int> quot_rem = IntegerDivide( 5, 4 ); cout << quot_rem; // "(1 1)" 另一方面,如果,你想发挥一下你才智的话,你可以控制括号,和分界符。 // Example 4(b): Customizing streamed tuples // tuple<int, int> quot_rem = IntegerDivide( 5, 4 ); cout << tuples::set_open('['] << tuples::set_close(')') << tuples::set_delimiter(',') << quot_rem; // "[1,1]" 你如果有兴趣,你可以参考Boost中tuple的实现,(www.boost.org), 下期预告: 在下一期里,我将详细讲解function机制 
|