3 回答
TA贡献1786条经验 获得超13个赞
is_base_of
template<typename T>class ObservableList { BOOST_STATIC_ASSERT((is_base_of<List, T>::value)); //Yes, the double parentheses are needed, otherwise the comma will be seen as macro argument separator ...};
template<typename T> class my_template; // Declare, but don't define// int is a valid typetemplate<> class my_template<int> { ...};// All pointer types are validtemplate<typename T> class my_template<T*> { ...};// All other types are invalid, and will cause linker error messages.
TA贡献1821条经验 获得超6个赞
<type_traits>
:
#include <type_traits>template<typename T>class observable_list { static_assert(std::is_base_of<list, T>::value, "T must inherit from list"); // code here..};
observable_list
const_iterator
begin
end
const_iterator
list
list
observable_list
.
static_assert
.
#include <type_traits>template<typename...>struct void_ { using type = void;};template<typename... Args>using Void = typename void_<Args...>::type;template<typename T, typename = void>struct has_const_iterator : std::false_type {};template<typename T>struct has_const_iterator<T, Void<typename T::const_iterator>> : std::true_type {};struct has_begin_end_impl { template<typename T, typename Begin = decltype(std::declval<const T&>().begin()), typename End = decltype(std::declval<const T&>().end())> static std::true_type test(int); template<typename...> static std::false_type test(...);};template<typename T>struct has_begin_end : decltype(has_begin_end_impl::test<T>(0)) {};template<typename T>class observable_list { static_assert(has_const_iterator<T>::value, "Must have a const_iterator typedef"); static_assert(has_begin_end<T>::value, "Must have begin and end member functions"); // code here...};
TA贡献1828条经验 获得超3个赞
int
static_assert
- 3 回答
- 0 关注
- 390 浏览
添加回答
举报