
On Wed, Oct 6, 2010 at 12:56 PM, alfC <alfredo.correa@gmail.com> wrote:
Hi,
Excuse my simple minded questions. Sometimes by asking this question (often with negative answer) I understand more on the design of the Boost libraries.
Besides possible redundancy, why is that the fusion containers, for example fusion::vector, doesn't have a template member function to extract elements?
fusion::vector<double, int, std::string> stuff(1.2, 3, "hola"); stuff.at<2>(); //returns "hola", just like at_c<2>(stuff);
Wouldn't it be more consistent with std::vector v, v[2] or v.at(2);
No, because things like std::vector is an implementation class, hence it can be done in any way they want, however fusion does not have that luxury, for example, boost::array is defined as a fusion container, so you can do something like this: template<typename T> void printSecondElement( const T &t ) { std::cout << *next(begin(t)); } Now, you can do something like this with it: fusion::vector<int,double,char> f(42,3.14,'X'); printSecondElement(f); Or you can do this: boost::array<int,3> a(1,2,3); printSecondElement(a); Or even this: struct MyType { char a; float f; } BOOST_FUSION_ADAPT_STRUCT( MyType, (char, a) (float, f) ) MyType t('r', 3.14); printSecondElement(t); And guess what, it prints the second element of each thing, whether a fusion::vector, a boost::array, or some custom MyType. Hence, it is impossible to have at be a member function. To be frank, you should *never* have any function be a member function unless it absolutely cannot be created externally, you will have better compile times, better encapsulation, and it prevents you from doing nice things like the above.