I need to develop an algorithm for a library that receives a “list of strings”.
Of course I want to pass an “abstract” iterator to decouple the internal representation of that list (a vector, a linked list, a map…) to the access to it from my function.
This is my approach:
template <typename ForwardIterator>
void myAlgorithm(ForwardIterator itBegin, ForwardIterator itEnd) {
for(; itBegin != itEnd ; ++itBegin) {
// Do something with *itBegin
}
}
My question is, how can I restrict the type of the container to be string? Or even more, is there some way of accepting both std::strings and char* as elements?
Thanks a lot.
Use a static assertion along with
std::iterator_traits<>:If you’re using a C++0x compiler, you can use
static_assertinstead ofBOOST_STATIC_ASSERTandstd::is_sameinstead ofboost::is_same.