在 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
发布评论