gpt4 book ai didi

c++ - 从先前的模板参数获取类型

转载 作者:行者123 更新时间:2023-11-30 02:09:25 33 4
gpt4 key购买 nike

我怎样才能实现这样的目标:

//where Low's parameter is of a type of Range's first parameter  
Range<char,Low<5>> range;

这里的问题我看到我希望/喜欢 Low 类型的参数取决于 Range 的第一个参数,例如:

template<class IntType,class LowType>
struct Range
{
};

并拥有:

template<class T>//how to do it that this parameter is of a type of Ranges first parameter?
struct Low
{
};

这可能吗?

最佳答案

部分特化将为您完成这项工作:

template<class IntType, class LowType>
struct Range;
//vvvvv
template<class IntType, template<class, IntType> class LowType, IntType N>
struct Range< IntType, LowType<IntType, N> >{
//^^^^^^^
// implementation here
};

编辑
它适用于上面显示的一些更改,但您需要更改您的 Low结构模板:

template<class IntType, IntType N>
struct Low{
};

并像显示的那样使用它 here on Ideone .但随之而来的问题是:为什么你需要一个额外的 Low结构?以下内容还不够吗?

template<class IntType, IntType Low>
struct Range;

如果你真的需要那个Low结构,您始终可以执行以下操作(使用上面显示的 Low 结构):

template<class IntType, IntType LowNum>
struct Range{
typedef Low<IntType,LowNum> LowType;
// ...
};

Range<int,5>一样使用它, 利用 Range在使用 Low 的同时为用户提供更方便的方式内部结构。

关于c++ - 从先前的模板参数获取类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5524561/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com