扣除指南和可变参数模板 [英] Deduction guide and variadic templates

查看:111
本文介绍了扣除指南和可变参数模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码:

#include <tuple>
#include <iostream>

template <class T>
struct custom_wrapper
{
    template <class Arg>
    custom_wrapper(Arg arg): data(arg) {}
    T data;
};

template <class Arg>
custom_wrapper(Arg arg) -> custom_wrapper<Arg>;

template <class... T>
struct custom_tuple
{
    template <class... Args>
    custom_tuple(Args... args): data(args...) {}
    std::tuple<T...> data;
};

template <class... Args>
custom_tuple(Args... args) -> custom_tuple<Args...>;

int main(int argc, char* argv[])
{
    custom_wrapper<int> w1(42);  // OK
    custom_wrapper w2(42);       // OK
    custom_tuple<int> t1(42);    // OK
    custom_tuple t2(42);         // Fails
    return 0;
}

失败的行在g ++ 7下返回以下错误:

The line that fails return the following error under g++7:

variadic_deduction_guide.cpp: In instantiation of 'custom_tuple<T>::custom_tuple(Args ...) [with Args = {int}; T = {}]':
variadic_deduction_guide.cpp:31:23:   required from here
variadic_deduction_guide.cpp:19:45: error: no matching function for call to 'std::tuple<>::tuple(int&)'
     custom_tuple(Args... args): data(args...) {}

是正常的还是编译器错误?

Is that normal or is that a compiler bug?

推荐答案

这是 gcc错误80871 。接下来是对为什么代码格式正确的解释(而clang认为 t2 custom_tuple< int> )。

This is gcc bug 80871. What follows is an explanation of why the code is well-formed (and clang is correct in deciding that t2 is a custom_tuple<int>).

弄清楚如何处理的过程

custom_tuple t2(42);

基本上涉及到合成一堆函数并对它们执行重载解析。相关的候选者是一个构造函数和推导指南的综合函数:

basically involves synthesizing a bunch of functions and performing overload resolution on them. The relevant candidates are the synthesized functions from the one constructor and the deduction guide:

template <class... T, class... Args>
custom_tuple<T...> foo(Args... );     // the constructor

template <class... Args>
custom_tuple<Args...> foo(Args... );  // the deduction guide

从这一点出发,根据您对 跟踪参数包根据 [temp.arg.explicit] / 3

From this point it's a choose your own adventure based on your interpretation of what a "trailing parameter pack" is according to [temp.arg.explicit]/3:


如果没有以其他方式推导的尾随模板参数包将被推导为空的模板参数序列。如果可以推导出所有模板参数,则可以全部省略;在这种情况下,空模板参数列表<> 本身也可以省略。

T ... 不是结尾

T... isn't trailing

这种情况简单。我们只有一个可行的候选人(因为 T ... 不可推论)-推论指南候选人。我们将 Args ... 推断为 {int} ,因此最终得到 custom_tuple< int>

This case is easy. We only have one viable candidate (because T... isn't deducible) - the deduction-guide candidate. We deduce Args... as {int}, so we end up with custom_tuple<int>.

T ... 正在跟踪

T... is trailing

gcc和clang实际上都考虑了对构造函数成功的推理。因此,我们进入 [over.match.best] 的决胜局:

Both gcc and clang actually do consider deduction to succeed for the constructor. So we go to the tiebreakers in [over.match.best]:


鉴于这些定义,将可行函数 F1 定义为更好函数,而不是另一个可行函数 F2 ,如果[...]

Given these definitions, a viable function F1 is defined to be a better function than another viable function F2 if [...]


  • F1 F2 是功能模板专业化,而 F1 <根据[temp.func.order]中描述的部分排序规则,/ code>比 F2 的模板更专业,或者,如果不是,则

  • F1 是从扣除指南([over.match.class.deduct])和 F2生成的不是,或者,如果不是这样,则[...]

  • F1 and F2 are function template specializations, and the function template for F1 is more specialized than the template for F2 according to the partial ordering rules described in [temp.func.order], or, if not that,
  • F1 is generated from a deduction-guide ([over.match.class.deduct]) and F2 is not, or, if not that, [...]

出于部分排序的目的,相关类型是只是那些ch对应于函数参数,并且允许我们忽略未使用的模板参数,因此这两个功能模板都不被认为比另一个功能模板更专业。

For purposes of partial ordering, the relevant types are just those which correspond to function parameters, and we're allowed to ignore unused template parameters, so neither function template is considered more specialized than the other.

这使我们只喜欢推导指南,这是最简单的步骤。这整个过程。我们将 Args ... 推断为 {int} ,因此最终得到 custom_tuple< int>

This leaves us to simply preferring the deduction-guide, which has been the simplest step of this whole process. We deduce Args... as {int}, so we end up with custom_tuple<int>.

无论哪种方式, custom_tuple< int> 是正确的决定。

Either way, custom_tuple<int> is the correct decision.

这篇关于扣除指南和可变参数模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆