jeudi 25 février 2016

meta function with default behavior on missing trait and how to detect random access

The following meta-function computes if a given type is a random access iterator:

    template <class I>
    struct is_random_access
        : boost::is_convertible
        < typename boost::iterator_traversal<I>::type
        , boost::random_access_traversal_tag
        >
    {};

Now of course, this doesn't work if I is not an iterator at all, as there is no boost::iterator_traversal<I> is defined.

Two independent questions:

  1. How to make is_random_access return false (rather than fail to compile) when I is not an iterator?
  2. Is this a good way to detect if an iterator is random access traverseable?

Aucun commentaire:

Enregistrer un commentaire