|
Boost : |
From: Marco Costalba (mcostalba_at_[hidden])
Date: 2008-06-04 16:43:15
Under fusion namespace we can write
vector<int, double, string> v(1, 2.4, "hello");
print_type( at_c<2>(v) ); // prints string
print_value( at_c<2>(v) ); // prints "hello"
But number 2 in our example must be a compile time constant, we cannot write
int idx = 2;
at_c<idx>(v); // compile error here
Sometime accessing an element in a fusion sequence indexed by a
runtime variable could be useful, something like the following:
vector<int, double, string> v(1, 2.4, "hello");
int idx;
cout << "Give me an index" << endl;
cin >> idx;
apply_at(v, idx, print_type); // prints string if idx == 2, or int if idx == 0
apply_at(v, idx, print_value); // prints "hello" if idx == 2, or 1 if idx == 0
Because I didn't find something similar in fusion and I needed that
for code I'm writing I cooked up my solution, it's very easy and if
someone is interested I can post it.
Marco
P.S: If someone could point me to an already available solution it
would be appreciated ;-) I just missed it.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk