I'm writing a pair
wrapper. For the purposes of this question it can be simplified down to:
using namespace std;
template <class T1, class T2>
class myPair {
pair<T1, T2> member;
public:
myPair() = default;
myPair(T1 x, T2 y) : member(make_pair(x, y)) {}
};
I'd like to be able to treat a myPair
as an index-able container of size 2. To do this I'd obviously need to write an index operator for myPair
. I'd like to do something like this, but my return type will depend upon a method parameter, and I can't use method parameters in meta-programming.
auto myPair::operator[](int index) {
static_assert(index >= 0 && index < 2, "Index out of range");
return get<index>(*this);
}
Obviously I could tackle this in the same way that pair
does by providing a get
function, but I'd like to me able to use the index operator syntax. Is there any way I can specialize a function template or use a method parameter to meta-program a template's return type?