【问题标题】:How to check if template type is of another template type?如何检查模板类型是否属于另一种模板类型?
【发布时间】:2016-08-07 04:58:41
【问题描述】:

假设我有一个带有 int 参数的模板类:

template <int Param>
class myclass
{
    // ...
}

例如,我想实现一个转换函数,从一个 int 参数化到另一个:

template <int Param, class T>
myclass<Param> myclass_cast(const T& other)
{
    // ... some logic
}

如何静态确定T 是模板类myclass&lt;N&gt;? (我可以使用 C++14)。

【问题讨论】:

    标签: c++ templates c++14


    【解决方案1】:

    你可以推导出你传入的myclass特化的参数,而不是T的模板参数:

    template <int Param, int Other>
    myclass<Param> myclass_cast (const myclass<Other>& other)
    {
    
    }
    

    【讨论】:

    • 哦,太好了,永远猜不到这个 :-)
    • 当参数是多个参数的模板时,它会如何表现?
    • @teivaz 我只有一个 int 参数,但这是一个有趣的问题
    • @teivaz 然后你需要推断出参数的数量,或者将myclass_cast 设为可变参数模板。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多