Skip to content

Instantly share code, notes, and snippets.

@2bbb
Last active March 13, 2017 22:25
Show Gist options
  • Select an option

  • Save 2bbb/00bed3e791da0518d8e784cb924d95d0 to your computer and use it in GitHub Desktop.

Select an option

Save 2bbb/00bed3e791da0518d8e784cb924d95d0 to your computer and use it in GitHub Desktop.
container_util
#include <iterator>
namespace bbb {
namespace detail {
template <typename _>
std::true_type make_true(_ = std::declval<_>());
};
template <typename checkee_type>
struct is_iteratable {
template <typename checkee>
static auto check(checkee c)
-> decltype(detail::make_true(std::begin(c)));
static auto check(...)
-> std::false_type;
using type = decltype(check(std::declval<checkee_type>()));
static constexpr bool value = type::value;
};
template <typename type>
using is_iteratable_t = typename is_iteratable<type>::type;
template <typename type>
static constexpr bool is_iteratable_f() { return is_iteratable<type>::value; }
template <typename checkee_type>
struct has_value_type {
template <typename checkee>
static auto check(checkee)
-> decltype(detail::make_true<typename checkee::value_type>());
static auto check(...)
-> std::false_type;
using type = decltype(check(std::declval<checkee_type>()));
static constexpr bool value = type::value;
};
template <typename type>
using has_value_type_t = typename has_value_type<type>::type;
template <typename type>
static constexpr bool has_value_type_f() { return has_value_type<type>::value; }
};
#include "iterator_utils.hpp"
#include <iostream>
#include <array>
#include <vector>
#include <map>
#include <string>
#include <memory>
struct P {
private:
P() {};
};
int main(int argc, char *argv[]) {
std::cout << bbb::is_iteratable<int>::value << std::endl;
std::cout << bbb::is_iteratable<std::vector<int>>::value << std::endl;
std::cout << bbb::is_iteratable<std::array<int, 4>>::value << std::endl;
std::cout << bbb::is_iteratable<std::map<int, int>>::value << std::endl;
std::cout << bbb::is_iteratable<std::string>::value << std::endl;
std::cout << bbb::is_iteratable<std::shared_ptr<int>>::value << std::endl;
std::cout << std::endl;
std::cout << bbb::has_value_type<int>::value << std::endl;
std::cout << bbb::has_value_type<std::vector<P>>::value << std::endl;
std::cout << bbb::has_value_type<std::array<int, 4>>::value << std::endl;
std::cout << bbb::has_value_type<std::map<int, int>>::value << std::endl;
std::cout << bbb::has_value_type<std::string>::value << std::endl;
std::cout << bbb::has_value_type<std::shared_ptr<int>>::value << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment