有`decltype(auto)`变量的实际用例吗? [英] Are there any realistic use cases for `decltype(auto)` variables?

查看:90
本文介绍了有`decltype(auto)`变量的实际用例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的亲身经历以及对问题的咨询性答复(例如) decltype(auto)?我可以找到很多有用的decltype(auto)用例作为函数返回类型占位符.

Both from my personal experience and from consulting answers to questions like What are some uses of decltype(auto)? I can find plenty of valuable use cases for decltype(auto) as a function return type placeholder.

但是,我在认真思考decltype(auto)变量的任何有效(即有用,现实,有价值)用例时都非常费劲.想到的唯一可能性是存储返回decltype(auto)的函数的结果以供以后传播,但是auto&&也可以在那里使用,并且会更简单.

However, I am seriously struggling to think of any valid (i.e. useful, realistic, valuable) use case for decltype(auto) variables. The only possibility that comes to mind is to store the result of a function returning decltype(auto) for later propagation, but auto&& could be used there as well and it would be simpler.

我什至搜索了我所有的项目和实验,并且decltype(auto)的391次出现都是返回类型占位符.

I've even searched throughout all my projects and experiments, and the 391 occurrences of decltype(auto) are all return type placeholders.

那么, decltype(auto)变量是否有实际的用例??或者此功能仅在用作返回类型占位符时有用吗?

So, are there any realistic use cases for decltype(auto) variables? Or it this feature only useful when used as a return type placeholder?

您如何定义现实"?

How do you define "realistic"?

我正在寻找一个提供值的用例(即,不仅仅是展示该功能如何工作的示例)其中,与auto&&之类的替代方案相比,decltype(auto)是最佳选择>或完全不声明变量.

I am looking for a use case that provides value (i.e. it's not just an example to show how the feature works) where decltype(auto) is the perfect choice, compared to alternatives such as auto&& or to not declaring a variable at all.

问题域无关紧要,可能是一些晦涩的元编程极端案例或神秘的功能编程构造.但是,该示例需要使我嘿,那很聪明/美丽!" ,并且使用任何其他功能来实现相同的效果将需要更多样板或有某种缺点.

The problem domain doesn't matter, it could be some obscure metaprogramming corner case or arcane functional programming construct. However, the example would need to make me go "Hey, that's clever/beautiful!" and using any other feature to achieve the same effect would require more boilerplate or have some sort of drawback.

推荐答案

可能不是很深的答案,但基本上是decltype(auto)

Probably not a very deep answer, but basically decltype(auto) was proposed to be used for return type deduction, to be able to deduce references when the return type is actually a reference (contrary to plain auto that will never deduce the reference, or auto&& that will always do it).

它也可以用于变量声明的事实并不一定意味着应该有一个比其他更好"的方案.确实,在变量声明中使用decltype(auto)只会使代码读取复杂化,因为对于变量声明而言,具有完全相同的含义.另一方面,auto&&形式使您可以声明一个常量变量,而decltype(auto)则不能.

The fact that it can also be used for variable declaration not necessarily means that there should be better-than-other scenarios. Indeed, using decltype(auto) in variable declaration will just complicate the code reading, given that, for a variable declaration, is has exactly the same meaning. On the other hand, the auto&& form allows you to declare a constant variable, while decltype(auto) doesn't.

这篇关于有`decltype(auto)`变量的实际用例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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