推导用户定义值模板参数(C ++ 2a,P0732R2) [英] Deducing a user-defined-value template argument (C++2a, P0732R2)

查看:111
本文介绍了推导用户定义值模板参数(C ++ 2a,P0732R2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试推导用户定义的类的模板参数的值( http:// wg21。 link / p0732r2 ),使用带有-std = c ++ 2a的GCC 9.1。

I am trying to get the value of a template parameter of a user-defined class deduced (http://wg21.link/p0732r2), using GCC 9.1 with -std=c++2a.

struct user_type {
   int a;
   constexpr user_type( int a ): a( a ){}
};

template< user_type u > struct value {};

template< user_type u > void f( value< u > arg ){}  

void g(){
  f( value< user_type( 0 ) >() ); // error here
}

编译器浏览器: https://godbolt.org/z/6v_p_R

我收到错误消息:

source>:8:30: note:   template argument deduction/substitution failed:
<source>:11:33: note:   couldn't deduce template parameter 'u'
   11 |    f( value< user_type( 0 ) >() );

我做错了吗?我原以为这样的值是可以扣除的。

Am I doing something wrong? I had expected such a value to be deductible.

按照Nikita的建议,我在用户类型中添加了==和!=运算符,但这没什么区别。

As suggested by Nikita I added == and != operators to user-type, but that made no difference.

struct user_type {
   int a;
   constexpr user_type( int a ): a( a ){}
   constexpr bool operator==( const user_type & arg ) const {
      return a == arg.a;
   }
   constexpr bool operator!=( const user_type & arg ) const {
      return a != arg.a;
   }
};


推荐答案

这应该格式错误:

struct user_type {
   int a;
   constexpr user_type( int a ): a( a ){}
};

template< user_type u > struct value {};

要成为模板非类型参数,您需要满足 [temp.param] / 4

In order to be a template non-type parameter, you need to satisfy [temp.param]/4:

非类型模板参数应具有以下(可选的cv限定)类型之一:

A non-type template-parameter shall have one of the following (optionally cv-qualified) types:


  • 具有强等式结构的文字类型([class.compare.default]),

  • [...]

需要强大的结构平等性的地方,来自 [class.compare.default] / 3

Where strong structural equality requires, from [class.compare.default]/3:


类型C具有很强的结构相等性如果给定const C类型的glvalue x,则:

A type C has strong structural equality if, given a glvalue x of type const C, either:


  • C是非类类型,并且[...]

  • C是类类型,其中在C的定义中默认定义了==运算符,x == x很好-在上下文转换为bool时形成,所有C的基类子对象和非静态数据成员都具有很强的结构相等性,并且C没有可变或不稳定的子对象。

  • C is a non-class type and [...], or
  • C is a class type with an == operator defined as defaulted in the definition of C, x == x is well-formed when contextually converted to bool, all of C's base class subobjects and non-static data members have strong structural equality, and C has no mutable or volatile subobjects.

关键是我们需要默认的 == 类型...而我们没有一个,因此我们的类型没有强大的结构相等性,因此不能用作模板非类型参数。

The key is that we need a defaulted == in the type... and we don't have one, so our type doesn't have strong structural equality, so it cannot be used as a template non-type parameter.

但是,gcc 尚未允许您声明这样的运算符,因此您无法解决问题。

However, gcc doesn't let you declare such an operator yet, so you can't fix the problem.

这只是新功能的不完全实现。

This is just an incomplete implementation of a new feature.

这篇关于推导用户定义值模板参数(C ++ 2a,P0732R2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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