c++ - 如何将模板类内部的模板类用作带有模板参数的模板模板参数?

标签 c++ templates c++11 stdtuple

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

我写的类似于“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 .

关于c++ - 如何将模板类内部的模板类用作带有模板参数的模板模板参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33490467/

相关文章:

c++ - 是否可以创建一个宏来在 C/C++ 中创建一致的函数声明?

c++ - OpenGL 延迟渲染不工作

c++ - 关于多重继承和歧义

c++ - 如何将 s.st_dev 转换为/sys/block/<name>

c++ - C++中显式赋值和隐式赋值有什么区别

c++ - 琐碎的整型 const 变量总是可以用作模板值吗?

c++ - C++:检测 vector 在函数中是否为<complex>类型

c# - 使用 C# 从 MS Word 中的加载项访问 RichTextContentControl 文本

C++11 main() 返回时终止线程?

OpenCV 4.x+ 需要启用 C++11 支持