I have a bunch of collection-like COM interfaces that I’m trying to write an STL-like iterator for. I’ve got the iterator working and specialized begin() and end() to return my iterator. Everything works perfectly! Except, when I try to use begin(std::vector), it’s using my very general specialization of begin(). Since these COM objects don’t extend from a base collection object, I first tried:
template< class CollType >
CollectionIterator<CollType> begin( CollType coll )
I see why the overload resolution isn’t picking the right begin() for std::vector, but I’m not sure how to fix this.
Unfortunately, I don’t have a base collection class to specialize the coll parameter against. I’m assuming I need something like SFINAE to only resolve these specializations if the right members exist. I tried:
template<
class CollType,
typename std::enable_if< std::is_member_pointer< decltype(&CollType::GetItem) >::value >::type
>
CollectionIterator<CollType> begin( CollType coll )
(where GetItem is a method of CollType)
As well as a handful of variations, to no avail. To make matters worse, these collections are COM smart pointers, so I’m not sure if GetItem will actually register as a member of the smart pointer.
Any insights into the right direction would be great, I’ve been mostly running in circles with this.
If you always use some specific COM smart pointer for your tricky collections (e.g.
_com_ptr_t<T>), you could definebegin()specialization this way:If this is not acceptable try another possible way (only if you use Visual C++ and don’t care to be non-standard) – using Microsoft specific extensions –
__if_exists/__if_not_existsstatements: