问题描述
在 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_reference 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!