为此编写一个真正通用的解决方案很困难。检查任意类型T
与std::vector
或std::array
的问题是后者不是类,它们是类模板。更糟糕的是,std::array
是一个带有非模板参数的类模板,所以你甚至不能拥有一个参数包,它可以容纳std::vector
和std::array
。
你可以通过在类型中显式包装非类型参数来解决这个问题,但它会变得很难看,速度很快。
这是我提出的一个解决方案,它将默认支持任何没有非模板参数的类或模板类。通过添加包装类型来将非类型参数映射到类型参数,可以支持具有非类型模板参数的模板类。
namespace detail{
//checks if two types are instantiations of the same class template
template<typename T, typename U> struct same_template_as: std::false_type {};
template<template<typename...> class X, typename... Y, typename... Z>
struct same_template_as<X<Y...>, X<Z...>> : std::true_type {};
//this will be used to wrap template classes with non-type args
template <typename T>
struct wrapImpl { using type = T; };
//a wrapper for std::array
template <typename T, typename N> struct ArrayWrapper;
template <typename T, std::size_t N>
struct ArrayWrapper<T, std::integral_constant<std::size_t, N>> {
using type = std::array<T,N>;
};
//maps std::array to the ArrayWrapper
template <typename T, std::size_t N>
struct wrapImpl<std::array<T,N>> {
using type = ArrayWrapper<T,std::integral_constant<std::size_t,N>>;
};
template <typename T>
using wrap = typename wrapImpl<typename std::decay<T>::type>::type;
//checks if a type is the same is one of the types in TList,
//or is an instantiation of the same template as a type in TempTList
//default case for when this is false
template <typename T, typename TList, typename TempTList>
struct one_of {
using type = std::false_type;
};
//still types in the first list to check, but the first one doesn't match
template <typename T, typename First, typename... Ts, typename TempTList>
struct one_of<T, std::tuple<First, Ts...>, TempTList> {
using type = typename one_of<T, std::tuple<Ts...>, TempTList>::type;
};
//type matches one in first list, return true
template <typename T, typename... Ts, typename TempTList>
struct one_of<T, std::tuple<T, Ts...>, TempTList> {
using type = std::true_type;
};
//first list finished, check second list
template <typename T, typename FirstTemp, typename... TempTs>
struct one_of<T, std::tuple<>, std::tuple<FirstTemp, TempTs...>> {
//check if T is an instantiation of the same template as first in the list
using type =
typename std::conditional<same_template_as<wrap<FirstTemp>, T>::value,
std::true_type,
typename one_of<T, std::tuple<>, std::tuple<TempTs...>>::type>::type;
};
}
//top level usage
template <typename T, typename... Ts>
using one_of = typename detail::one_of<detail::wrap<T>,Ts...>::type;
struct Foo{};
struct Bar{};
template <class Type>
auto operator<< (std::ostream& stream, const Type subject)
//is Type one of Foo or Bar, or an instantiation of std::vector or std::array
-> typename std::enable_if<
one_of<Type, std::tuple<Foo,Bar>, std::tuple<std::vector<int>,std::array<int,0>>
>::value, std::ostream&>::type
{
stream << "whatever, derived from subject\n";
return stream;
}
请不要使用这个,这太可怕了。的[C++模板,仅接受某些类型](
Live Demo
可能重复的http://计算器。COM /问题/ 874298/C的模板,也就是说,接受只,某些类型) –
的问题是,我想包括类型,如性病::阵列,性病::阵列,性病::阵列, ......这就是一个无穷级数不同类型的....不知道是否有一种方法可以做到这一点.. –
我仍然无法找到答案,如何防止实例化编译器从我的模板函数,当我使用cout << int(5)。当断言失败时,static_assert或BOOST_STATIC_ASSERT将生成一个编译错误,而不是忽略我的模板。 –