Ever wish you could do something like this in C++? Create a function that takes one parameter and returns two values?
const char * fileBegin;
const char * fileEnd;
(fileBegin, fileEnd) = AFunctionThatReturnsAPairOfStrings(fileName);
It is possible, with a slight modification. Returning two items in a struct or class is, of course, not a new idea. But I want the maximum ease for extracting the results and assigning them to variables.
Update: This is exactly what Boost tie does.
const char * fileBegin;
const char * fileEnd;
make_refpair(fileBegin, fileEnd) = mmapFile(fileName);
I've defined a simple class and an accompanying function make_refpair to allow this, which I describe below. It seems like an obvious solution, but I can't quickly find anybody who's done this before. E.g. they miss this on stackoverflow. Of course, there is nothing novel in returning a pair<>, but I want the convenience of assigning both variables to the contents of the returned pair. Anybody seen something similar/better?
Aaron
template <class T1, class T2>
struct RefPair {
T1& e1;
T2& e2;
RefPair(T1 &in1, T2 &in2) : e1(in1), e2(in2) { }
void operator= (pair<T1, T2> source) { e1 = source.first; e2 = source.second; }
};
template <class T1, class T2>
RefPair<T1, T2> make_refpair(T1 &e1, T2 &e2) {
RefPair<T2, T2> rf(e1,e2);
return rf;
}
Recent comments
10 weeks 6 days ago
13 weeks 6 days ago
14 weeks 5 days ago
19 weeks 2 days ago
19 weeks 2 days ago
33 weeks 9 hours ago
44 weeks 1 day ago
44 weeks 1 day ago
45 weeks 4 days ago
45 weeks 4 days ago