yu *_*uan 5 c++ templates c++11
这里说我有一个简单的模板功能,原则上可以接受所有类型:
template <class Type>
std::ostream& operator<< (std::ostream& stream, const Type subject) {
stream << "whatever, derived from subject\n";
return stream; }
Run Code Online (Sandbox Code Playgroud)
我只想用这个模板来讨论几种类型,比如std :: vector和boost :: array对象.但是每当我使用cout到其他类型甚至基本类型时,例如std :: cout << int(5);,都会出现编译错误,因为现在有两种可能的operator <<(std :: ostream,int)实现一个是标准的c ++,另一个是我的模板函数.
我想问一下,是否可以限制我的模板功能,以便它只接受我指定的几种类型?这是当我使用cout << int(5)时告诉编译器忽略我的模板的方法.提前致谢.
更清楚,这就是我想要做的:
template <class Type>
std::ostream& operator<< (std::ostream& stream, const Type subject) {
if (Type == TypeA or TypeB or TypeC) //use this template and do these {...};
else //ignore this template, and use operator<< provided in standard c++ library.
}
Run Code Online (Sandbox Code Playgroud)
为此编写一个非常通用的解决方案很难.与检查任意类型的问题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;
}
Run Code Online (Sandbox Code Playgroud)
请不要使用它,这太可怕了.
你可以这样限制你的超载:
template <class T>
std::ostream& my_private_ostream( std::ostream& stream, const T& data )
{ <your implementation> }
template <class T, class A>
std::ostream& operator<< ( std::ostream& stream, const std::vector<T,A>& data )
{ return my_private_ostream(stream,data); }
Run Code Online (Sandbox Code Playgroud)
s也是如此std::array(你应该用 c++11 标记你的问题):
template <class T, size_t N>
std::ostream& operator<< ( std::ostream& stream, const std::array<T,N>& data )
{ return my_private_ostream(stream,data); }
Run Code Online (Sandbox Code Playgroud)
或者,对于看起来更像您的编辑的解决方案,您可以使用 C++11 enable_if,尽管我个人厌恶它们,因为它们往往会使代码难以阅读和维护。所以我强烈推荐之前的解决方案。
// Vector type predicate
template <class T>
struct is_vector: std::false_type {};
template <class T, class A>
struct is_vector< std::vector<T,A> >: std::true_type {};
// Array type predicate
template <class T>
struct is_array: std::false_type {};
template <class T, size_t N>
struct is_array< std::array<T,N> >: std::true_type {};
// The overload with the syntax you want
template <class Indexable>
typename std::enable_if<
is_vector<Indexable>::value || is_array<Indexable>::value,
std::ostream&
>::type
operator<< ( std::ostream& stream, const Indexable& data )
{ <your implementation> }
Run Code Online (Sandbox Code Playgroud)