使用模板进行递归类型检查
Recursive type check using templates
我有几个类型检查模板:来自 type_traits
的 is_object
和 isSupportedContainer
以下列方式实现:
template <class T>
struct isSupportedContainer
: public false_type {};
template <class T>
struct isSupportedContainer < list<T> >
: public true_type {};
/*other containers*/
我想进行递归检查 isSupported
不仅将自身应用于容器类型,而且应用于包含的类型。当前实施:
// using std::__and_ and std::for from type_traits
template <class T>
struct isSupported
: public __and_ < __or_ < is_object<T>, isSupportedContainer<T> >,
isSupported <T> > {};
当我调用isSupported < vector<int> >::value
时,它产生了一堆编译错误(缩写):
In instantiation of 'struct std::__and_<...>, isSupportedContainer<std::vector<...> > >, isSupported<std::vector<...> > >':
required from 'struct isSupported<std::vector<int> >'
required from /*its call in main()*/
error: invalid use of incomplete type 'std::conditional<true, isSupported<std::vector<int> >, std::__or_<...>, isSupportedContainer<std::vector<...> > > >::type'
struct __and_<_B1, _B2>
^
In /*file containing isSupported and isSupportedContainer*/:
error: declaration of 'std::conditional<true, isSupported<std::vector<int> >, std::__or_<...> >::type'
struct isSupported
^
In function 'int main()':
error: 'value' is not a member of 'isSupported<std::vector<int> >'
cout << isSupported < vector<int> >::value;
^
那么,如何实现这样的检查呢?
示例:假设 list
和 vector
受支持 类 vector<list<int>>
也受支持,vector<list<vector<string>>>
不受支持
UPD:工作版本
UPD2:不,不工作
template <class T>
struct isSupported
: public isSupportedSimpleObject<T> {}; //is_object turned out to be a wrong thing
template <template<class> class T, class U>
struct isSupported < T<U> >
: public __and_ < isSupportedContainer<T<U>>,
isSupported <U> > {};
如果您在初始替换中使用 enable_if
,您可能会简化整个计算。
#include <type_traits>
#include <list>
#include <vector>
#include <string>
template <class T>
struct isSupportedContainer : std::false_type {};
template <class T>
struct isSupportedContainer < std::list<T> > : std::true_type {};
template <class T>
struct isSupportedContainer < std::vector<T> > : std::true_type {};
template <class T, typename = void> // #1
struct isSupported : std::integral_constant<bool, std::is_object<T>::value> {};
template <class Cont> // #2
struct isSupported<Cont, typename std::enable_if<isSupportedContainer<Cont>::value>::type>
: isSupported<typename Cont::value_type> {};
int main() {
static_assert(isSupported<std::vector<int>>::value,"");
static_assert(isSupported<std::vector<std::list<int>>>::value,"");
static_assert(isSupported<std::string>::value,"");
static_assert(!isSupported<int&>::value,"");
return 0;
}
此技术(如果它确实是您所追求的)基于将 std::void_t
添加到 C++17 的原理
重点如下:
- 主模板定义接受两个类型参数。第一个命名为
T
,另一个未命名,默认类型为 void
。这个版本只是检查你的基本情况。
- 实例化模板时,编译器匹配主实例
isSupported<YourType, void>
。
现在编译器检查是否有任何与参数类型 <YourType, void>
匹配的特化,所以它会查看提供的特化。 std::enable_if
用于确定它的类型实际上是受支持的容器,如果是这样它 returns void
,我们匹配最初推断的类型 <YourType, void>
。因此编译器将专业化作为更好的匹配,我们进行递归检查。
- 但是如果提供的类型不是受支持的容器,
std::enable_if
没有 type
成员,SFINAE 开始。替换失败,我们 return 到基本情况我们在 (1) 中找到了它,它进行了基本检查。
我有几个类型检查模板:来自 type_traits
的 is_object
和 isSupportedContainer
以下列方式实现:
template <class T>
struct isSupportedContainer
: public false_type {};
template <class T>
struct isSupportedContainer < list<T> >
: public true_type {};
/*other containers*/
我想进行递归检查 isSupported
不仅将自身应用于容器类型,而且应用于包含的类型。当前实施:
// using std::__and_ and std::for from type_traits
template <class T>
struct isSupported
: public __and_ < __or_ < is_object<T>, isSupportedContainer<T> >,
isSupported <T> > {};
当我调用isSupported < vector<int> >::value
时,它产生了一堆编译错误(缩写):
In instantiation of 'struct std::__and_<...>, isSupportedContainer<std::vector<...> > >, isSupported<std::vector<...> > >':
required from 'struct isSupported<std::vector<int> >'
required from /*its call in main()*/
error: invalid use of incomplete type 'std::conditional<true, isSupported<std::vector<int> >, std::__or_<...>, isSupportedContainer<std::vector<...> > > >::type'
struct __and_<_B1, _B2>
^
In /*file containing isSupported and isSupportedContainer*/:
error: declaration of 'std::conditional<true, isSupported<std::vector<int> >, std::__or_<...> >::type'
struct isSupported
^
In function 'int main()':
error: 'value' is not a member of 'isSupported<std::vector<int> >'
cout << isSupported < vector<int> >::value;
^
那么,如何实现这样的检查呢?
示例:假设 list
和 vector
受支持 类 vector<list<int>>
也受支持,vector<list<vector<string>>>
不受支持
UPD:工作版本
UPD2:不,不工作
template <class T>
struct isSupported
: public isSupportedSimpleObject<T> {}; //is_object turned out to be a wrong thing
template <template<class> class T, class U>
struct isSupported < T<U> >
: public __and_ < isSupportedContainer<T<U>>,
isSupported <U> > {};
如果您在初始替换中使用 enable_if
,您可能会简化整个计算。
#include <type_traits>
#include <list>
#include <vector>
#include <string>
template <class T>
struct isSupportedContainer : std::false_type {};
template <class T>
struct isSupportedContainer < std::list<T> > : std::true_type {};
template <class T>
struct isSupportedContainer < std::vector<T> > : std::true_type {};
template <class T, typename = void> // #1
struct isSupported : std::integral_constant<bool, std::is_object<T>::value> {};
template <class Cont> // #2
struct isSupported<Cont, typename std::enable_if<isSupportedContainer<Cont>::value>::type>
: isSupported<typename Cont::value_type> {};
int main() {
static_assert(isSupported<std::vector<int>>::value,"");
static_assert(isSupported<std::vector<std::list<int>>>::value,"");
static_assert(isSupported<std::string>::value,"");
static_assert(!isSupported<int&>::value,"");
return 0;
}
此技术(如果它确实是您所追求的)基于将 std::void_t
添加到 C++17 的原理
重点如下:
- 主模板定义接受两个类型参数。第一个命名为
T
,另一个未命名,默认类型为void
。这个版本只是检查你的基本情况。 - 实例化模板时,编译器匹配主实例
isSupported<YourType, void>
。
现在编译器检查是否有任何与参数类型<YourType, void>
匹配的特化,所以它会查看提供的特化。std::enable_if
用于确定它的类型实际上是受支持的容器,如果是这样它 returnsvoid
,我们匹配最初推断的类型<YourType, void>
。因此编译器将专业化作为更好的匹配,我们进行递归检查。 - 但是如果提供的类型不是受支持的容器,
std::enable_if
没有type
成员,SFINAE 开始。替换失败,我们 return 到基本情况我们在 (1) 中找到了它,它进行了基本检查。