Неповторяющиеся типы, используемые в качестве параметров шаблона - PullRequest
3 голосов
/ 29 октября 2010

Учитывая приведенный ниже код, есть ли лучший способ исправить его, который не повторяется typename std::iterator_traits<T>::iterator_category дважды?

template<class T, class T2>
struct foo :
    bar<
        foo<T, T2>, typename 
        std::conditional<
            std::is_same<typename 
                std::iterator_traits<T>::iterator_category,  //Repeated
                std::random_access_iterator_tag
            >::value,
            std::bidirectional_iterator_tag, typename 
            std::iterator_traits<T>::iterator_category       //Repeated
        >::type
    >
{}

Ответы [ 2 ]

3 голосов
/ 29 октября 2010

Разделить это (как и должно быть в любом случае):

// put in detail namespace/file or something in real code
template<class T, class T2>
struct foo_base
{
    typedef foo<T, T2> foo_type;
    typedef typename std::iterator_traits<T>::iterator_category category_type;

    static const bool random_access = std::is_same<category_type,
                                        std::random_access_iterator_tag>::value;
    typedef typename std::conditional<random_access,
                                        std::bidirectional_iterator_tag,
                                        category_type>::type tag_type;

    typedef bar<foo_type, tag_type>::type base_type;
}

template<class T, class T2>
struct foo :
   foo_base<T, T2>::base_type
{};

Даже если не было повторного бита, вы все равно должны разделить его, чтобы отделить логику базового типа от фактического наследования базового типа.

0 голосов
/ 29 октября 2010

Вы могли бы typedef это:

typedef std::iterator_traits<T>::iterator_category it_cat;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...