考虑一个例子:
#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 回答
是的,这是符合标准的 .
根据[dcl.type.simple]/2:
并[temp.param]/3:
TT
是使用template
声明的类型参数,它使其成为模板名称,因此是推导类类型的占位符 . 所有通常的规则都适用 .