什么时候比其他模板更专业? '和'/'或'与逻辑混淆。 [英] When is a template more specialized than the other? 'And'/'Or' confusion with logics.

查看:161
本文介绍了什么时候比其他模板更专业? '和'/'或'与逻辑混淆。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11草稿的14.8.2.4p10中,写入了


如果对于每个类型都被认为是给定的模板至少对于所有类型是专门的,并且对于某些类型的集合更专门,并且其他模板不是更专用于任何类型,或者至少不是对于任何类型专门化,则给定模板比其他模板更专业化。


为什么有或者至少不是专门为任何类型?就我所见,如果我们有一个类型列表

  T1,T2,T3 
U1,U2 ,U3

并且如果所有的Ts至少如同专业化,并且我们没有一个更专业,那么在我看来,在整个T的集合比U的集合更加专业化,从逻辑上讲。为什么那时提到的后备,当我们没有一个至少像相应的Ts?

解决方案

更新:现在已添加为官方C ++问题






我终于找出了如何阅读这段话。


如果对于每个类型,给定的模板至少与所有类型一样专用,


  • 更专用于某些类型的类型,而其他模板不是更专用于任何类型,

  • 其他模板}不是至少与任何类型相同,



那么给定的模板比其他模板更专业。 / p>

这样,以下第一个模板比第二个模板更专业

  template< typename T> void f(T *); 
template< typename T> void f(T);

请注意,第一个模板的参数至少与第二个模板相同, 更专业化 - 该术语仅适用于两个参数均为参考文献和某些条件适用的情况(见14.8.2.4第9段)。规则显然不是意在遵循任何正式的排序法律。第二模板至少不如第一模板那样专门。这意味着第二个项目符合,而不是第一个。


In 14.8.2.4p10 of the C++11 draft, there is written

If for each type being considered a given template is at least as specialized for all types and more specialized for some set of types and the other template is not more specialized for any types or is not at least as specialized for any types, then the given template is more specialized than the other template.

Why is there a "or is not at least as specialized for any types"? As far as I can see, if we have a list of types

T1, T2, T3
U1, U2, U3

And if all Ts are at least as specialized and some are more specialized. And none of the Us are more specialized, then it seems to me that it follows that the set of T as a whole is more specialized than the set of U, logically speaking. Why is there then that mentioned fallback for when none of the Us are at least as specialized than the corresponding Ts?

解决方案

Update: This has now been added as an official C++ Issue


I have finally figured out how to read the paragraph in question. Below I have bulleted it

If for each type being considered a given template is at least as specialized for all types, and

  • more specialized for some set of types and the other template is not more specialized for any types, or
  • {the other template} is not at least as specialized for any types,

then the given template is more specialized than the other template.

This way the following first template is also more specialized than the second template

template<typename T> void f(T*);
template<typename T> void f(T);

Note that the first template's parameter is at least as specialized as the second template, but is not defined to be "more specialized" - that term only applies for the case where both parameters were references and certain conditions apply (see paragraph 9 of 14.8.2.4) . The rules are apparently not meant to follow any formal ordering laws. The second template is not at least as specialized as the first template. This means that the second bullet applies, and not the first.

这篇关于什么时候比其他模板更专业? '和'/'或'与逻辑混淆。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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