编译时的模板参数计算 [英] Template argument calculation at compile time

查看:23
本文介绍了编译时的模板参数计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在编译时推导出两个模板参数中较大的一个.两个模板参数都是 size_t 类型.

I'm trying to deduce the greater of two template arguments at compile time. Both template arguments are of type size_t.

我有一个模板化类型 SomeType,它采用 size_t 作为模板参数.然后我有一个函数,它采用两个具有不同模板 size_t 的 SomeType 参数,我希望返回类型是 SomeType,其模板化 size_t 是两个输入 size_t 大小中较大的一个.

I have a templated type, SomeType, which takes a size_t as it's template argument. I then have a function that takes two SomeType parameters with different template size_t's and i want the return type to be a SomeType with its templated size_t to be the greater of the two input size_t sizes.

template <size_t d> struct SomeType {...}

template<size_t d1, size_t d2>
SomeType<the_larger_of_d1_and_d2> Func(SomeType<d1> A, SomeType<d2> B)
{
    ...
}

这可能吗?

推荐答案

可以直接计算类型,不需要SFINAE:

You can compute the type directly, no need for SFINAE:

template<size_t d1, size_t d2>
SomeType<(d1 > d2 ? d1 : d2)> Func(SomeType<d1> A, SomeType<d2> B)
{
    …
}

这篇关于编译时的模板参数计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆