C++0x: How can I access variadic tuple members by index at runtime?

后端 未结 4 670
既然无缘
既然无缘 2021-01-14 07:38

I have written the following basic Tuple template:

template 
class Tuple;

template 
struct TupleIndex         


        
4条回答
  •  太阳男子
    2021-01-14 08:18

    You do the exact same thing as you do with TupleIndexer, just at runtime.

    Add a function like this to the Tuple class:

    Head &operator[](unsigned i) {
        return i ? ((Tuple&)*this)[i-1] : element;
    }
    

    and add a specialization for Tuple:

    Head &operator[](unsigned i) {
        assert(!i);
        return i;
    }
    

    (You can't put the base case into Tuple<>, since you don't have a type to return that would be compatible with every possible caller.)

提交回复
热议问题