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

W.F*_*.F. 8 c++ templates language-lawyer template-argument-deduction c++17

考虑一个例子:

#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();
}
Run Code Online (Sandbox Code Playgroud)

在推导模板模板参数(#1)的模板参数时,[clang][gcc]似乎都使用用户提供的演绎指南(#2).它是标准兼容功能吗?

Bar*_*rry 5

是的,这符合标准。

根据[dcl.type.simple]/2

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

[temp.param]/3

标识符不跟在省略号后面的类型参数将其标识符定义为模板声明范围内的typedef-name(如果不使用 声明template)或template-name(如果使用 声明)。template

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