默认模板参数在部分专业化背景下的作用 [英] Role of default template arguments in the context of partial specialization

查看:145
本文介绍了默认模板参数在部分专业化背景下的作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不清楚在部分专业化的情况下默认模板参数的交互作用,因为选择哪个是更好的匹配模板.此问题源自此

I am not clear about the interaction of default template arguments in the context of partial specialization, for choosing which is the better matching template. This questions stems from code posted in this answer by max66.

给出类AB的定义:

template <int N> struct A { static const int code = N; };

struct B{};

和以下模板类:

// primary template
template <typename, typename Enable = bool_constant<true>>
struct cond : public bool_constant<false> {};

// specialization
template <typename T>
struct cond<T, bool_constant<(0 == T::code)>> : public bool_constant<true> {};

1)cond<B>::value得出false(即选择了主要对象).这很明显,因为主模板产生cond<B, bool_constant<true>>,专业化失败了,因此主模板是唯一可能的选择.

1) cond<B>::value evaluates to false (i.e. the primary is chosen). This is clear as the primary template yields cond<B, bool_constant<true>>, the specialization fails, hence the primary template is the only possible choice.

2)cond<A<0>>::value得出true(即选择了专业化).这很明显,因为主模板产生cond<B, bool_constant<true>>,专业化也产生cond<B, bool_constant<true>>,因此首选专业化,因为明确指定了第二个模板参数的参数.

2) cond<A<0>>::value evaluates to true (i.e. the specialization is chosen). This is clear as the primary template yields cond<B, bool_constant<true>>, the specialization also yields cond<B, bool_constant<true>>, hence the specialization is preferred because the argument for the 2nd template parameter is explicitly given.

3)cond<A<1>>::value的结果为false(即选择了主要的).我不清楚.主模板产生cond<B, bool_constant<true>>,专业化产生cond<B, bool_constant<false>>.给定第二个模板参数的参数已在专业化中明确给出,为什么不首选?

3) cond<A<1>>::value evaluates to false (i.e. the primary is chosen). This is not clear to me. The primary template yields cond<B, bool_constant<true>>, the specialization yields cond<B, bool_constant<false>>. Given the argument for the 2nd template parameter is explicitly given in the specialization, why is not preferred?

我想(3)中的行为是由于主模板的默认模板参数和专业化之间的某种相互作用.杰里·科芬(Jerry Coffin)在此答案中陈述了一些可以解释这种行为的东西:

I suppose the behaviour in (3) is due to some interaction between the default template argument of the primary template and the specialization. In this answer Jerry Coffin states something which might explain this behaviour:

如果我们更改专业化以使其专业化 用于基本模板提供的默认类型以外的其他类型 然后将选择基本模板.

if we change the specialization so that its specialization is for a type other than the default provided by the base template then the base template will be chosen.

有人可以详细说明这个规则吗?谢谢

Can somebody please elaborate on this rule? Thanks

推荐答案

template <typename, typename Enable = bool_constant<true>>
struct cond : public bool_constant<false> {};

等同于

template <typename, typename Enable = bool_constant<true>> struct cond;

template <typename, typename Enable>
struct cond : public bool_constant<false> {};

以后可能更清楚地了解结果.

Later might be clearer to understand the result.

编写cond<C>时,由于使用了默认参数,所以它等效于cond<C, bool_constant<true>>.

When you write cond<C>, thanks to default argument, it is equivalent to cond<C, bool_constant<true>>.

然后我们尝试将其与更好的实例化" 相匹配.

Then we try to match that to "better instantiation".

我们可以选择:

// primary template
template <typename, typename Enable>
struct cond : public bool_constant<false> {};

和部分专业化,它们使用SFINAE:

and partial specialization, which use SFINAE:

// specialization
template <typename T>
struct cond<T, bool_constant<(0 == T::code)>> : public bool_constant<true> {};

如果0 == T::code格式不正确,则将放弃专业化,只有主要模板才是可行的解决方案,因此可以使用.

If 0 == T::code is ill formed, specialization is discarded and only primary template is a viable solution, so it is used.

否则,如果0 == T::code计算为false,则说明专业化不匹配,并且还使用了主模板.
请注意,在这种情况下,使用cond<C, bool_constant<false>>将使用专业化.

Else if 0 == T::code evaluates to false, the specialization doesn't match and primary template is also used.
Note that using cond<C, bool_constant<false>> would use the specialization in that case.

否则,0 == T::code取值为true,则初级和专业化都是可行的,但专业化则更加专业化,因此选择了它.

Else, 0 == T::code evaluates to true, and then both primary and specialization are viable, but specialization is more specialized, so it is chosen.

这篇关于默认模板参数在部分专业化背景下的作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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