I’m writing a class which will be used to perform some calculations on a set of values, with scaling based on a per-value weight. The values and weights are supplied to the class’ constructor. The class will be part of an internal library, and so I want to put as few restrictions as possible on the clients data structures – some clients will use vectors of structs or std::pairs, another separate OpenCV matrixes. During development I’ve taken start/end iterators and relied on the pair mechanism (val = it->first, weight = it->second).
How could this be done better, without too much hassle for the programmer on the other end? Generally, what is considered best practise when having this sort of multi-dimensional input?
Iterators are fine. However, relying on the types having public members called
firstandsecondis a pretty big restriction.In C++0x, access to
std::pairmembers will be unified with the access patterns ofstd::tuple, via agetfunction. This would allow you to overload and specialize the get function for arbitrary types:In case your standard library doesn’t have
getforpair, then boost’s tuple library seems to have it.