是否保证模板模板参数调用用户提供的演绎指南 [英] Is it guaranteed that template template parameter invoke user provided deduction guides

查看:70
本文介绍了是否保证模板模板参数调用用户提供的演绎指南的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个例子:

#include <type_traits>
#include <string>

template <template <class> class TT> //#1
struct Foo {
   static void foo() {
      static_assert(std::is_same_v<decltype(TT("abc")), TT<std::string>>);
   }
};

template <class T>
struct Bar {
    Bar(T) {}
};

template <class T>
Bar(T) -> Bar<std::string>; //#2

int main() {
    Foo<Bar>::foo();
}

[clang] 以及

[clang] as well as [gcc] both seem to use user provided deduction guides (#2) when deducing the template parameter of template template parameter (#1). Is it a standard compliant feature?

推荐答案

是的,这是符合标准的.

Yes, this is standard compliant.

根据 [dcl.type.simple]/2 :

格式为typename opt 类型说明符 嵌套名称说明符 opt template-name 是推断的类类型([dcl.type.class.deduct])的占位符. template-name 应该命名不是注入类名称的类模板.

A type-specifier of the form typenameopt nested-name-specifieropt template-name is a placeholder for a deduced class type ([dcl.type.class.deduct]). The template-name shall name a class template that is not an injected-class-name.

[temp.param]/3 :

其标识符不带省略号的 type-parameter 将其标识符定义为 typedef-name (如果声明时未使用template)或 template -name (如果用template声明)在模板声明的范围内.

A type-parameter whose identifier does not follow an ellipsis defines its identifier to be a typedef-name (if declared without template) or template-name (if declared with template) in the scope of the template declaration.

TT是用template声明的类型参数,这使其成为 template-name ,因此是推导类类型的占位符.所有通常的规则都适用.

TT is a type-parameter declared with template, which makes it a template-name and hence a placeholder for a deduced class type. All the usual rules apply just fine.

这篇关于是否保证模板模板参数调用用户提供的演绎指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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