Skip to content

Instantly share code, notes, and snippets.

@komiga
Created November 3, 2014 05:38
Show Gist options
  • Save komiga/686f3239d35bbe8fe8ad to your computer and use it in GitHub Desktop.
Save komiga/686f3239d35bbe8fe8ad to your computer and use it in GitHub Desktop.
assign_match mumbo jumbo for tinch_pp
#include <type_traits>
#include <memory>
template<class>
struct is_smart_pointer : std::false_type {};
template<class T>
struct is_smart_pointer<std::shared_ptr<T>> : std::true_type {};
template<class T>
struct is_smart_pointer<std::unique_ptr<T>> : std::true_type {};
static_assert(!is_smart_pointer<int>::value, "");
static_assert(is_smart_pointer<std::shared_ptr<int>>::value, "");
static_assert(is_smart_pointer<std::unique_ptr<int>>::value, "");
template<class T, bool = is_smart_pointer<typename T::value_type>::value>
struct list_matcher_assign_match;
template<class T>
struct list_matcher_assign_match<T, false> {
static bool func(T* val) {
// definition for normal objects
typename T::value_type to_assign;
// ...
return false;
}
};
template<class T>
struct list_matcher_assign_match<T, true> {
static bool func(T* val) {
// definition for smart pointers
typename T::value_type::element_type to_assign;
// ...
return true;
}
};
template<class T>
struct list_matcher {
static bool assign_match(T* val /* other params */) {
return list_matcher_assign_match<T>::func(val);
}
};
#include <cassert>
#include <list>
signed main() {
assert(!list_matcher<std::list<int>>::assign_match(nullptr));
assert(list_matcher<std::list<std::shared_ptr<int>>>::assign_match(nullptr));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment