如何选择部分模板专业化? [英] How partial template specialization chosen?

查看:78
本文介绍了如何选择部分模板专业化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请向我解释模板专业化选择的规则。我有一个例子:

Please explain me the rules for template specialization selection. I have an example:

template<typename T1, typename T2 = int>
struct S : false_type{};

template<typename T>
struct S<T, float> : true_type{};

cout << boolalpha << S<float>::value;

为什么输出为 false ?通常,在特殊类中使用默认模板参数 typename T2 = int 会发生什么?

Why the output is false? And in general, what happens with default template parameter typename T2 = int in specialized classes? Does it introduces some influence?

推荐答案

选择模板特化分五个步骤:

Choosing a template specialization happens in five steps:


  1. 执行主模板声明。 (< T1,T2 = int> S

  2. 填充用户指定的模板参数。 ( T1<-浮动

  3. 仅功能模板:推导其他模板参数。

  4. 对其余模板参数使用默认值。 ( T2<-int

  5. 使用偏序算法(C ++ 14 14.5.6.2)选择最佳-匹配的专业化。 (< float,int> < T,float> 不匹配,因此请忽略专业化;只有可能性左边是主要模板)

  1. Take the primary template declaration. (<T1, T2 = int> S)
  2. Fill in user-specified template arguments. (T1 <- float)
  3. Function templates only: Deduce additional template arguments.
  4. Use defaults for remaining template arguments. (T2 <- int)
  5. Use the partial ordering algorithm (C++14 14.5.6.2) to choose the best-matching specialization. (<float, int> does not match <T, float>, so ignore the specialization; only possibility left is primary template)

这篇关于如何选择部分模板专业化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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