Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从模板化类中提取类型_C++_Templates_Types_C++14 - Fatal编程技术网

C++ 从模板化类中提取类型

C++ 从模板化类中提取类型,c++,templates,types,c++14,C++,Templates,Types,C++14,我正在为任何模板化类实现一个类型提取器extract\u type。示例用法如下所示: template <int, short, float, double, char> class C; extract_type<C, 0>::type => int extract_type<C, 1>::type => short extract_type<C, 2>::type => float extract_type<C, 3

我正在为任何模板化类实现一个类型提取器
extract\u type
。示例用法如下所示:

template <int, short, float, double, char> class C;

extract_type<C, 0>::type => int
extract_type<C, 1>::type => short
extract_type<C, 2>::type => float
extract_type<C, 3>::type => double
extract_type<C, 4>::type => char
模板类C;
提取类型::类型=>int
提取类型::类型=>short
提取类型::类型=>float
提取类型::类型=>double
提取类型::类型=>char
这是我的实现

// extract_type: recursive definition.
template <template <typename...> class C, size_t idx, typename T, typename... RestT> 
struct extract_type;

// extract_type: base
template <template <typename...> class C, typename T, typename... RestT>
struct extract_type< C<RestT...>, 0, RestT... > { 
  using type = T;
};


// extract_type: recursive definition.
template <template <typename...> class C, size_t idx, typename T, typename... RestT>
struct extract_type : public extract_type< C<RestT...>, idx-1, RestT... > { 
};
//提取类型:递归定义。
模板
结构提取类型;
//提取类型:基
模板
结构提取类型{ 
使用类型=T;
};
//提取类型:递归定义。
模板
结构提取类型:公共提取类型{ 
};
然而,编译器抱怨

“模板类C,长无符号整数idx,类T,类…”的模板参数列表中参数1处的类型/值不匹配。。。RestT>struct OpenCluster::提取类型' 结构提取类型


我怎样才能解决这个问题?

你的意思是这样的(最低限度,工作示例)

因此,它将被用作:

extract_type<1, int, double, char>::type
提取类型::类型
结果相同:
double


你的例子中有错误吗(当然除了语法错误)?

你的意思是这样的(最小的,有效的例子)

因此,它将被用作:

extract_type<1, int, double, char>::type
提取类型::类型
结果相同:
double


在您的示例中得到了错误(当然除了语法错误)?

这是我的实现

#include "iostream"

template<class ...Ts> struct C {};

template<int N, class ...Ts>
struct extract_type_impl;

template<class C, int N>
struct extract_type;

template<template<class ...> class C, class ...Ts, int N>
struct extract_type<C<Ts...>, N> {
  typedef typename extract_type_impl<N, Ts...>::type type;
};

template<int N, class T, class ...Ts>
struct extract_type_impl<N, T, Ts...> {
  typedef typename extract_type_impl<N - 1, Ts...>::type type;
};

template<class T, class ...Ts>
struct extract_type_impl<0, T, Ts...> {
  typedef T type;
};

int main() {
  static_assert(std::is_same<extract_type<C<int, float, char, double>, 3>::type, double>::value, "");
  // static_assert(std::is_same<extract_type<C<int, float, char, double>, 3>::type, int>::value, "");
}
#包括“iostream”
模板结构C{};
模板
结构提取\u类型\u impl;
模板
结构提取类型;
模板
结构提取类型{
typedef typename extract_type_impl::type type;
};
模板
结构提取类型导入{
typedef typename extract_type_impl::type type;
};
模板
结构提取类型导入{
T型;
};
int main(){
静态断言(std::is_same::value,“”);
//静态断言(std::is_same::value,“”);
}
  • 获取C的参数列表
  • 获取第n个参数

这是我的实现

#include "iostream"

template<class ...Ts> struct C {};

template<int N, class ...Ts>
struct extract_type_impl;

template<class C, int N>
struct extract_type;

template<template<class ...> class C, class ...Ts, int N>
struct extract_type<C<Ts...>, N> {
  typedef typename extract_type_impl<N, Ts...>::type type;
};

template<int N, class T, class ...Ts>
struct extract_type_impl<N, T, Ts...> {
  typedef typename extract_type_impl<N - 1, Ts...>::type type;
};

template<class T, class ...Ts>
struct extract_type_impl<0, T, Ts...> {
  typedef T type;
};

int main() {
  static_assert(std::is_same<extract_type<C<int, float, char, double>, 3>::type, double>::value, "");
  // static_assert(std::is_same<extract_type<C<int, float, char, double>, 3>::type, int>::value, "");
}
#包括“iostream”
模板结构C{};
模板
结构提取\u类型\u impl;
模板
结构提取类型;
模板
结构提取类型{
typedef typename extract_type_impl::type type;
};
模板
结构提取类型导入{
typedef typename extract_type_impl::type type;
};
模板
结构提取类型导入{
T型;
};
int main(){
静态断言(std::is_same::value,“”);
//静态断言(std::is_same::value,“”);
}
  • 获取C的参数列表
  • 获取第n个参数

相关:
模板类C
是一个模板参数,它突然成为空间化中的类型模板参数。此外,
,这些是非类型模板参数,因此
C类
甚至不能与
模板C类
匹配,您确定要
提取类型
,而不是
提取类型
?相关:
模板C类
是模板参数,它突然成为空间化中的类型模板参数。此外,
,这些是非类型模板参数,因此
C类
甚至无法与
模板C类
匹配。您确定要
提取类型
,而不是
提取类型
?Hi Skypjack。谢谢你的帮助@您使用的是
typename。。。在您的代码中rest
。这是什么意思?这是一个,还有什么?是的。我明白了。我在声明模板专门化时犯了一个错误。嗨,Skypjack。谢谢你的帮助@您使用的是
typename。。。在您的代码中rest
。这是什么意思?这是一个,还有什么?是的。我明白了。我在声明模板专用化时犯了一个错误。什么是对继承的反对?@skypjack:当类型的继承树超过一定数量的字符时,某些编译器(*cough*VC++*cough*)会发出警告。除此之外,这只是个人偏好。什么反对继承?@skypjack:当类型的继承树超过一定数量的字符时,某些编译器(*cough*VC++*cough*)会发出警告。除此之外,这只是个人喜好。
#include "iostream"

template<class ...Ts> struct C {};

template<int N, class ...Ts>
struct extract_type_impl;

template<class C, int N>
struct extract_type;

template<template<class ...> class C, class ...Ts, int N>
struct extract_type<C<Ts...>, N> {
  typedef typename extract_type_impl<N, Ts...>::type type;
};

template<int N, class T, class ...Ts>
struct extract_type_impl<N, T, Ts...> {
  typedef typename extract_type_impl<N - 1, Ts...>::type type;
};

template<class T, class ...Ts>
struct extract_type_impl<0, T, Ts...> {
  typedef T type;
};

int main() {
  static_assert(std::is_same<extract_type<C<int, float, char, double>, 3>::type, double>::value, "");
  // static_assert(std::is_same<extract_type<C<int, float, char, double>, 3>::type, int>::value, "");
}