Created
April 24, 2015 20:12
-
-
Save mirekfranc/7565a2d722a66f220b17 to your computer and use it in GitHub Desktop.
dispatch based on type of the iterator the container supports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <deque> | |
#include <list> | |
#include <set> | |
#include <unordered_set> | |
#include <forward_list> | |
#include <iterator> | |
template<typename C> | |
void containter_access (C c, std::random_access_iterator_tag) | |
{ | |
std::cout << "container with random access" << std::endl; | |
} | |
template<typename C> | |
void containter_access (C c, std::bidirectional_iterator_tag) | |
{ | |
std::cout << "container with bidirectional access" << std::endl; | |
} | |
template<typename C> | |
void containter_access (C c, std::forward_iterator_tag) | |
{ | |
std::cout << "container with forward access" << std::endl; | |
} | |
template<typename C> | |
void containter_access (C c) | |
{ | |
containter_access (c, typename std::iterator_traits<typename C::iterator>::iterator_category ()); | |
} | |
int main () | |
{ | |
std::vector<int> v; | |
containter_access (v); | |
std::deque<int> d; | |
containter_access (d); | |
std::list<int> l; | |
containter_access (l); | |
std::set<int> s; | |
containter_access (s); | |
std::unordered_set<int> us; | |
containter_access (us); | |
std::forward_list<int> fl; | |
containter_access (fl); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment