首页 文章

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

提问于
浏览
8

考虑一个例子:

#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 回答

  • 4

    是的,这是符合标准的 .

    根据[dcl.type.simple]/2

    typenameopt nested-name-specifieropt template-name形式的类型说明符是推导类类型的占位符([dcl.type.class.deduct]) . template-name应命名一个不是注入类名的类模板 .

    [temp.param]/3

    标识符不遵循省略号的类型参数将其标识符定义为模板声明范围内的typedef-name(如果声明为无模板)或template-name(如果使用模板声明) .

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

相关问题