1

Пытаюсь написать класс, реализующий одновременно константный и неконстантый итератор как показано здесь.

Проблема возникает при компиляции следующего участка кода(упрощено):

class SimpleIt {
    using my_vector_ref_type = named_vector<T>&;
public:
    SimpleIt(my_vector_ref_type r) {}
};

//То же самое, но с std::conditional
template <bool is_const>
class CondIt {
    using my_vector_ref_type = std::conditional<
            is_const,
            const named_vector<T>&,
            named_vector<T>&
    >;
public:
    CondIt(my_vector_ref_type r) {}
};

iterator begin() {
    SimpleIt si(*this); //Компилирует
    CondIt<false> c(*this); //error: no matching constructor for initialization of 'CondIt<false>'

PS: http://tpcg.io/FBny1V

1 ответ 1

1

Во-первых нужен #include <type_traits>, во вторых нужно имя типа:

using my_vector_ref_type = typename ::std::conditional
<
        is_const
,       const named_vector<T>&
,       named_vector<T>&
>::type;

А если используется с++14, то можно написать короче:

using my_vector_ref_type = ::std::conditional_t
<
        is_const
,       const named_vector<T>&
,       named_vector<T>&
>;
1
  • фууух, у меня бомбило уже, спасибо
    – NoTrust
    7 окт 2018 в 17:07

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.