Created
February 22, 2016 21:49
-
-
Save pudov/0b1572989c418684c381 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // inspired by https://gist.github.com/oliora/b8207b4a74a6bb868ef4 | |
| // a bit less text, less standard way | |
| #include <cassert> | |
| #include <utility> | |
| #include <algorithm> | |
| #include <type_traits> | |
| template <typename T> | |
| constexpr bool has_own_find(...) { return false; } | |
| template <typename T> | |
| constexpr bool has_own_find(decltype(std::declval<T>().find(std::declval<typename T::key_type>()))* = 0) { return true; } | |
| template<class Cont, class T> | |
| auto do_find(const Cont& c, const T& value) -> typename std::enable_if<!has_own_find<Cont>(nullptr), typename Cont::const_iterator>::type | |
| { | |
| using std::begin; | |
| using std::end; | |
| return std::find(begin(c), end(c), value); | |
| } | |
| template<class Cont, class T> | |
| auto do_find(const Cont& c, const T& value) -> typename std::enable_if<has_own_find<Cont>(nullptr), typename Cont::const_iterator>::type | |
| { | |
| return c.find(value); | |
| } | |
| #include <vector> | |
| #include <set> | |
| int main(int argc, const char *argv[]) | |
| { | |
| std::vector<int> v; | |
| auto itv = do_find(v, 42); | |
| assert (itv == v.end()); | |
| std::set<int> s; | |
| auto its = do_find(s, 42); | |
| assert (its == s.end()); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment