简体   繁体   English

无操作函数模板

[英]no-op function template

I need to implement a no-op in C++ which preserves all type extensions (const, rvalue ref, lvalue ref).我需要在 C++ 中实现一个 no-op,它保留所有类型扩展(const、rvalue ref、lvalue ref)。 Can I do this as follows?我可以这样做吗?

template<class T>
T && noop (T && t) { return std::forward<T> (t); }

and call it like并称之为

... noop (value) ...

(ie use type inference)? (即使用类型推断)? Ie does noop (value) have exactly the same type as value ?noop (value)value类型完全相同吗?

Yes it does.是的,它确实。 However, it's no different from just calling std::forward in the client code itself (except that with std::forward there's no automatic template argument deduction).但是,它与仅在客户端代码本身中调用 std::forward 没有什么不同(除了 std::forward 没有自动模板参数推导)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM