是否保证模板模板参数调用用户提供的演绎指南

时间:2017-10-06 16:43:44

标签: c++ templates language-lawyer c++17 template-deduction

考虑一个例子:

#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();
}
在推断模板模板参数(#1)的模板参数时,

[clang]以及[gcc]似乎都使用用户提供的演绎指南(#2)。它是标准兼容的功能吗?

1 个答案:

答案 0 :(得分:4)

是的,这是符合标准的。

根据[dcl.type.simple]/2

  

{em}类型说明符,格式为typename opt nested-name-specifier opt template-name 是推断类类型的占位符([dcl.type.class.deduct])。 template-name 应命名一个不是注入类名的类模板。

[temp.param]/3

  

type-parameter ,其标识符不遵循省略号,将其标识符定义为 typedef-name (如果声明没有template)或 template-name (如果用template声明)在模板声明的范围内。

TT是使用template声明的类型参数,它使其成为模板名称,因此是推断类类型的占位符。所有通常的规则都适用。