如何使用模板类内部的模板类作为带有模板参数的模板模板参数
How can I use template class inside of template class as a template template parameter with a template parameter?
我正在尝试使用一个模板类作为另一个模板参数的模板模板参数。听起来,它非常复杂/扭曲,我不知道如何修复编译器错误。
我写的类似于"std::find_if",但它适用于"std::tuple"。如下面的代码所示,当我为is_same_type_tt指定类型时,它似乎可以工作,但当我尝试使用is_same_type_tt的模板参数时,编译器会抱怨:
main.cpp:75:13: error: type/value mismatch at argument 2 in template parameter list for 'template<class TupleType, template<class> class Action> struct tuple_find_if_tt'
>::value;
^
源代码如下:
template< typename TypeLookingFor >
struct is_same_type_tt
{
typedef TypeLookingFor type_looking_for;
template< typename TypeCompareTo >
struct type_tt : is_same< type_looking_for, TypeCompareTo >
{};
};
// base for the recusion
template<
typename TupleType
, size_t Index
, template< typename > class Action >
struct tuple_find_if_recur_tt;
// last in the recursion
template<
typename TupleLast
, size_t Index
, template< typename > class Action >
struct tuple_find_if_recur_tt< tuple< TupleLast >, Index, Action >
: conditional<
Action< TupleLast >::value
, integral_constant< size_t, Index >
, integral_constant< size_t, -1 > >::type
{};
// recursion
template<
typename TupleHead, typename... TupleRest
, size_t Index
, template< typename > class Action >
struct tuple_find_if_recur_tt<
tuple< TupleHead, TupleRest... >, Index, Action >
: conditional<
Action< TupleHead >::value
, integral_constant< size_t, Index >
, tuple_find_if_recur_tt<
tuple< TupleRest... >
, Index + 1u
, Action > >::type
{};
// wrap the recursion
template<
typename TupleType
, template< typename > class Action >
struct tuple_find_if_tt
: tuple_find_if_recur_tt< TupleType, 0u, Action >
{};
作为测试,以下代码编译良好:
// this works fine.
static_assert(
tuple_find_if_tt< tuple< int, float, double >
, is_same_type_tt< float >::type_tt >::value == 1, "" );
但当我尝试将它与另一个模板参数一起使用时,它不起作用:
// problem starts from here...
template< typename... Types >
struct tuple_indirect_find_tt
{
typedef tuple< Types... > tuple_type;
// tuple_type obj_;
template< typename TypeLookingFor >
static constexpr size_t find()
{
return tuple_find_if_tt<
tuple_type
// something is not right below...
, typename is_same_type_tt< TypeLookingFor >::type_tt
>::value;
}
};
// this doesn't work.
static_assert(
tuple_indirect_find_tt< int, float, double >::find< float >()
== 1, "" );
为了让它发挥作用,我不得不重构模板类,这样我就可以这样做:
return tuple_find_if_tt<
tuple_type
, is_same_type_tt
, typename TypeLookingFor // this is separated from is_same_type_tt
>::value;
变通办法似乎还不错,但我仍然想知道我做错了什么。如果这不是一种可能的方法,我想知道是什么C++标准阻止了它
感谢阅读。
typename is_same_type_tt< TypeLookingFor >::type_tt
type_tt
不是类型。你的谎言混淆了编译器,使它认为它与template<class>class
参数不匹配。
尝试is_same_type_tt< TypeLookingFor >::template type_tt
。
这里我们说依赖名称type_tt
是template
,而不是typename
。
相关文章:
- SFINAE 内部概念模板参数
- 作为模板模板参数的内部模板类
- 自动参数如何在内部工作
- 如何使结构内部的可变参数模板可以在外部访问?
- 如何处理可能指向内部数据的参考参数
- 如何从对象内部的 typedef 获取模板参数类型
- 是定义的函数参数的内部执行顺序
- 用相同的参数将内部和外部构造函数分开
- 宏 htonl 将内部逗号解释为参数分隔符
- 参数内部和外部的指针
- 将模板参数传递到内部结构时出错
- 函数和内部函数相同的参数
- 从内部类型参数推断外部类型
- C :如何通过其构造函数的参数初始化类内部矩阵的尺寸
- _mm_extract_epi8(..) 内部函数,采用非文字整数作为参数
- 从函数内部更改参数
- 定义变量以保存内部参数
- 如何通过参数内部的引用传递对象
- 模板化结构的友元函数,其参数类型取决于结构的内部
- 在for语句参数内部的C/C++算术运算中