是否保证模板模板参数调用用户提供的推导指南
Is it guaranteed that template template parameter invoke user provided deduction guides
考虑一个例子:
#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] as well as [gcc] 在推导模板模板参数 (#1) 的模板参数时,似乎都使用了用户提供的推导指南 (#2)。它是否符合标准?
是的,这符合标准。
A type-specifier of the form typename
opt 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.
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
声明的类型参数,这使它成为 模板名称 ,因此是推导的 [=29] 的占位符=] 类型。所有通常的规则都适用。
考虑一个例子:
#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] as well as [gcc] 在推导模板模板参数 (#1) 的模板参数时,似乎都使用了用户提供的推导指南 (#2)。它是否符合标准?
是的,这符合标准。
A type-specifier of the form
typename
opt 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.
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 withtemplate
) in the scope of the template declaration.
TT
是用 template
声明的类型参数,这使它成为 模板名称 ,因此是推导的 [=29] 的占位符=] 类型。所有通常的规则都适用。