I've never used boost::type_traits personally, but I would think it
would work. Not sure though. If not, you could define a template and
then specialize accordingly. I gave it a try, thinking most STL
iterators would be derived from std::iterator:
#include <iterator>
template < typename Type >
struct is_iterator_type
{
enum { value = false };
};
template < class Category, class Type, class Distance, class Pointer,
class Reference >
struct is_iterator_type< std::iterator< Category, Type, Distance,
Pointer, Reference > >
{
enum { value = true };
};
template < typename Type >
struct is_iterator_type< Type* >
{
enum { value = true };
};
template < typename Type >
struct is_iterator_type< Type const* >
{
enum { value = true };
};
/*
Helper function that deduces the type of some value
*/
template < typename Type >
inline bool is_iterator( Type const& )
{
return is_iterator_type< Type >::value;
}
// Test:
#include <cassert>
#include <list>
#include <vector>
int main( void )
{
using namespace
std;
char*
pc = 0;
char const*
pcc = 0;
char const* const
pccc = 0;
list< char >
lc;
vector< char >
vc;
assert( is_iterator( pc ) );
assert( is_iterator( pcc ) );
assert( is_iterator( pccc ) );
assert( is_iterator( lc.begin( ) ) );
assert( is_iterator( vc.rbegin( ) ) );
return 0;
}
Unfortunately, the assertion failed on the container iterators. So you
would need to specialize is_iterator_type for every possibility. And
as far as detected a container type, again, neither do these have a
common base class, so you would have to define and specialize a
template for every know type.
- hth