“decltype(auto)"变量是否有任何实际用例? [英] Are there any realistic use cases for `decltype(auto)` variables?

查看:42
本文介绍了“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.

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

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?

您如何定义现实"?

我正在寻找一个提供价值的用例(即它不仅仅是展示该功能如何工作的示例)其中 decltype(auto) 是完美的选择, 与 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) 被提议用于返回类型推导,以便在返回类型时推导引用实际上是一个引用(与永远不会推断出引用的普通 auto 或总是会推断出引用的 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) 只会使代码阅读复杂化,因为对于变量声明,is 具有完全相同的含义.另一方面,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天全站免登陆