“Generic” iterator in c++

后端 未结 7 1212
慢半拍i
慢半拍i 2021-01-04 06:09

I have:

void add_all_msgs(std::deque::iterator &iter);

How can I make that function \"generic\", so it can take any kind

相关标签:
7条回答
  • 2021-01-04 07:14

    If you want the compiler to check whether the iterator actually refers to Message objects, you can use a technique like the following.

    template <typename InputIterator, typename ValueType>
    struct AddAllMessages { };
    
    template <typename InputIterator>
    struct AddAllMessages<InputIterator, Message> {
      static void execute(const InputIterator &it) {
        // ...
      }
    };
    
    template <typename InputIterator>
    void add_all_msgs(const InputIterator &it) {
      AddAllMessages<InputIterator, 
                     typename std::iterator_traits<InputIterator>::value_type>::execute(it);
    }
    
    0 讨论(0)
提交回复
热议问题