std::decay 和 std::remove

编程入门 行业动态 更新时间:2024-10-24 03:25:00
本文介绍了std::decay 和 std::remove_reference 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在 C++ 中进行模板元编程时,我经常遇到如下情况:

When template metaprogramming in C++, I often run into something like the following:

template <typename T> S<T> make_wrapper(T&& t) { return S<T>(std::forward<T>(t)); }

我知道我应该在返回类型中使用类似 std::decay 的东西,但是为什么 std::remove_reference 不能正常工作?这里有什么区别?std::remove_cvref 怎么样?

I know I should use something like std::decay in the return type, but why wouldn't std::remove_reference work as well? What's the difference here? What about std::remove_cvref?

推荐答案

删除引用会留下 const 和 volatile.如果这就是你想要的,那就足够了.

Removing reference would leave const and volatile. If that is what you want, then it will suffice.

删除 cvref 完成了衰减所做的大部分工作,但不会将函数类型和数组类型转换为指针.

Removing cvref does most of what decay does, but doesn't convert function types and array types to pointers.

decay 以某种方式转换类型,您可以合理地将其副本存储在数组或 struct 中,或者从函数返回或将其传递给函数.

decay converts a type in a way that you could reasonably store a copy of it in an array or in a struct, or return it from or pass it to a function.

更多推荐

std::decay 和 std::remove

本文发布于:2023-08-04 17:33:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1298256.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:std   decay   remove

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!