concept(std::bidirectional_iterator) not working

I am trying to implement C++20 concept in a std::list implementation of mine.

Here is the simplified version of the code that will just highlight the part where an error is thrown.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <concepts>
#include <iterator>

using namespace std;

template <typename T>
class List
{
    struct Node
    {
        T data;
        Node* prev = nullptr;
        Node* next = nullptr;

        Node() = default;
        Node(const T& d) : data(d), prev(nullptr), next(nullptr) {}
        Node(T&& d) : data(d), prev(nullptr), next(nullptr) {}
        template<typename... Args> Node(Args&&... args) : data(std::forward<Args>(args)...) {}
    };

    Node* head = nullptr;
    Node *tail = nullptr;
    std::size_t l_size = 0;

public:
    using reference =                  T&;
    using const_reference =            const T&;
    using size_type =                  std::size_t;

    class const_iterator;

    class iterator : public std::bidirectional_iterator_tag
    {

    };

    class const_iterator
    {

    };

    using reverse_iterator =              std::reverse_iterator<iterator>;
    using const_reverse_iterator =        std::reverse_iterator<const_iterator>;

    List() = default;
    template<std::bidirectional_iterator InputIterator>
    List(InputIterator first, InputIterator last)
    {

    }



    // iterators
    iterator begin() noexcept { };
    iterator end() noexcept { };


    template<std::bidirectional_iterator InputIterator>
    iterator insert(const_iterator pos, InputIterator first, InputIterator last)
    {

    }

};

int main()
{
   List<int> lis;
   List<int> lis1(lis.begin(), lis.end()); // error here

   auto iter = lis.begin();

   lis.insert(iter, lis1.begin(), lis1.end()) // error here
}


But it throws errors in the constructor and in the insert method.

1
2
3
4
5
6
7
8
9
10
11

error: no matching function for call to 'List<int>::List(List<int>::iterator, List<int>::iterator)'|
note: candidate: 'List<T>::List(InputIterator, InputIterator) [with InputIterator = List<int>::iterator; T = int]'|
note: constraints not satisfied|
In instantiation of 'List<T>::List(InputIterator, InputIterator) [with InputIterator = List<int>::iterator; T = int]':|
required from here|
  required for the satisfaction of 'input_or_output_iterator<_Iter>' [with _Iter = List<int>::iterator<int>]|
  required for the satisfaction of 'input_iterator<_Iter>' [with _Iter = List<int>::iterator<int>]|
  required for the satisfaction of 'forward_iterator<_Iter>' [with _Iter = List<int>::iterator<int>]|
  required for the satisfaction of 'bidirectional_iterator<InputIterator>' [with InputIterator = List<int>::iterator<int>]|
error: no matching function for call to 'List<int>::insert(List<int>::iterator&, List<int>::iterator, List<int>::iterator)'|



Last edited on
In order to satisfy input_iterator, a type must do more than inherit std::bidirectional_iterator_tag. It must supply operator* and such with the right types, etc.

Your old SFINAE-based approach performed the minimum checking required to prevent ambiguity. Concept checking looks a lot more closely at the involved types, and you're falling afoul of it.

You'll need to implement your iterator before it will satisfy the concept. Specifically, you must implement the operations discussed in this reference page
https://en.cppreference.com/w/cpp/iterator/input_iterator
If that page is too inscrutable, read the old definition of input iterators:
https://en.cppreference.com/w/cpp/named_req/InputIterator
which is hopefully better.

In any event std::list::iterator is defined to satisfy these requirements, so you could just implement it without paying attention to the requirements in those pages.

This paper talks more about concepts - in particular, the problems it's supposed to solve, and how to properly use the feature:
https://www.stroustrup.com/good_concepts.pdf
Last edited on
Thanks, I will take a look at them and get back to you
Topic archived. No new replies allowed.