隐式模板推导指南可以推导引用类型吗? [英] Could an implicit template deduction guide deduce a reference type?

查看:73
本文介绍了隐式模板推导指南可以推导引用类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用gcc7测试C ++ 17演绎指南行为时,我发现此示例失败:

While testing C++17 deduction guide behaviour with gcc7, I found that this example fails:

template<class T>
struct S{
  S(T&& v){}
};
int i=10;
auto v = S(i);

根据我从 cpp参考,我认为 v 的类型应为 S< ; int& 。但是,gcc7不会编译该代码,抱怨 int& 不能绑定到 int&&

According to what I have read from cpp reference, I thought v should be of type S<int &>. Nevertheless gcc7 does not compile this code complaining that a int& can not be bound to a int && (the universal reference mechanism fails).

所以我的问题是:


  1. gcc7是否应推导 v S< int&> 类型?

在工作草案标准中描述了自动扣除指南?

Where are described automatic deduction guide in the working draft standard?


推荐答案

[over.match .class.deduct] 是:


形成了一组函数和功能模板,包括:

-对于由模板名称指定的主类模板的每个构造函数,如果定义了模板,则该函数模板具有以下属性:

    -模板参数是模板templa的模板参数te后跟构造函数的模板参数(包括默认模板参数)(如果有的话)。

    -函数参数的类型是构造函数的类型。

    -返回类型是由模板名称和与从类模板获得的模板参数相对应的模板参数指定的类模板特化。

A set of functions and function templates is formed comprising:
- For each constructor of the primary class template designated by the template-name, if the template is defined, a function template with the following properties:
    - The template parameters are the template parameters of the class template followed by the template parameters (including default template arguments) of the constructor, if any.
    - The types of the function parameters are those of the constructor.
    - The return type is the class template specialization designated by the template-name and template arguments corresponding to the template parameters obtained from the class template.

我们的集合包括:

template <class T> // <-- the template parameters come from the class template
S<T>               // <-- the return type is the class template specialization   
foo(T&& );         // <-- the types of the parameters are those of the constructor

通常,这涉及模板推导。 但是来自 [temp.deduct.call]

We perform overload resolution as usual, which involves template deduction. But from [temp.deduct.call]:


转发引用是对cv不合格模板参数的右值引用,该参数不表示类模板的模板参数(在类模板参数推导([over.match.class.deduct]期间))。如果P是转发引用,并且参数是左值,则使用类型对A的左值引用代替A进行类型推导。

A forwarding reference is an rvalue reference to a cv-unqualified template parameter that does not represent a template parameter of a class template (during class template argument deduction ([over.match.class.deduct])). If P is a forwarding reference and the argument is an lvalue, the type "lvalue reference to A" is used in place of A for type deduction.

因此,该 T& 不是是转发参考。它是对 T 的右值引用。因此,对左值(在我们的例子中为 S(i))进行推导失败。 gcc在这里拒绝您的代码是正确的。

Hence, this T&& is not a forwarding reference. It is an rvalue reference to T. So deduction against an lvalue (in our case, S(i)) fails. gcc is correct to reject your code here.

如果您希望类模板参数用作转发参考,则需要添加一个推导指南:

If you want the class template parameter to function as a forwarding reference, you will need to add a deduction guide:

template <class T> S(T&& ) -> S<T>;

这篇关于隐式模板推导指南可以推导引用类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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