M
Matt Fioravante
Suppose you wanted to write something like this:
template <typename T, typename CMP=std::less<T> >
class SortedVector : public std::vector<T> {
//stuff
}
Whats the best way to "store" the CMP object?
One could make a private data member CMP _cmp for example.
However this seems to waste space when the comparison is a free function such
as the the default std::less and it will store a function pointer to this
method in _cmp. If you pass it a function, it should not use any storage to
record this compile time constant information.
However if you want to pass it a lambda or some other function object, you may
have to store a copy of said object to retain its state.
So basically what it boils down to is if the function object contains state,
then store that state in the container. If it does not contain state, make the
address of the function a compile time constant in the template argument.
Whats the trick here to do this properly?
template <typename T, typename CMP=std::less<T> >
class SortedVector : public std::vector<T> {
//stuff
}
Whats the best way to "store" the CMP object?
One could make a private data member CMP _cmp for example.
However this seems to waste space when the comparison is a free function such
as the the default std::less and it will store a function pointer to this
method in _cmp. If you pass it a function, it should not use any storage to
record this compile time constant information.
However if you want to pass it a lambda or some other function object, you may
have to store a copy of said object to retain its state.
So basically what it boils down to is if the function object contains state,
then store that state in the container. If it does not contain state, make the
address of the function a compile time constant in the template argument.
Whats the trick here to do this properly?