【问题标题】:Is it guaranteed that template template parameter invoke user provided deduction guides是否保证模板模板参数调用用户提供的推导指南
【发布时间】:2017-10-06 16:43:44
【问题描述】:

考虑一个例子:

#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][gcc] 在推导模板模板参数(#1)的模板参数时,似乎都使用了用户提供的推导指南(#2)。它是符合标准的功能吗?

【问题讨论】:

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


    【解决方案1】:

    是的,这是符合标准的。

    根据[dcl.type.simple]/2

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

    还有[temp.param]/3:

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

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

    【讨论】:

    • 我其实预料到了这个答案,但如果它碰巧是错误的......那么我会收回它;)
    • 作为一个小问题:你认为自动扣除指南也不应该适用吗?
    • @W.F.好吧,我完全颠倒了答案:)
    • 不错的发现!看 - 仍然不必收回它! :)
    猜你喜欢
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 2022-12-22
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    • 2018-07-17
    相关资源
    最近更新 更多