如何使用模板类内部的模板类作为带有模板参数的模板模板参数

How can I use template class inside of template class as a template template parameter with a template parameter?

本文关键字:参数 内部 何使用      更新时间:2023-10-16

我正在尝试使用一个模板类作为另一个模板参数的模板模板参数。听起来,它非常复杂/扭曲,我不知道如何修复编译器错误。

我写的类似于"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_tttemplate,而不是typename