如何像boost :: unwrap

编程入门 行业动态 更新时间:2024-10-26 09:18:21
本文介绍了如何像boost :: unwrap_reference一样解开std :: reference_wrapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我打算移植一些代码,这些代码对C ++ 11经常使用boost :: unwrap_reference。该代码调用了很多成员函数,例如

template<类型名T,类型名Y> void initialize(T _t,Y y) { typename boost :: unwrap_reference< T> :: type& t = _t; t.doSomethingNastyWithY(y); } //函数的调用方式如下: struct DoSomething { template< typename Y> void doSomethingNastyWithY(Y y) { //做东西} }; 结构对象{}; DoSomething s; 对象obj; int main() { initialize(s,obj); //取得DoSomething的副本 initialize(boost :: ref(s),obj); //使用DoSomething作为参考}

我找不到等同于boost的值: :STL中的unwrap_reference,还有其他直接方法可以做到这一点吗?

编辑:我对示例进行了澄清

解决方案

类似于以下内容的东西:

template<类型名称T> struct UnwrapReference; 模板<类型名称T> struct UnwrapReference {typedef T type; } 模板< > 结构体UnwrapReference< std :: reference_wrapper< & > {typedef T type; }

虽然未经测试,但这就是您大概能够做到的要点。 / p>

I am tring to port some code, that uses boost::unwrap_reference a lot to C++11. The code calls a lot member functions, like

template< typename T, typename Y> void initialize( T _t, Y y) { typename boost::unwrap_reference< T >::type & t = _t; t.doSomethingNastyWithY( y ); } // The function is called like this struct DoSomething { template<typename Y> void doSomethingNastyWithY(Y y) { // do stuff } }; struct Object {}; DoSomething s; Object obj; int main() { initialize( s, obj ); // Take a copy of DoSomething initialize( boost::ref(s), obj ); // Uses DoSomething as reference }

I couldn't find an equivalent to boost::unwrap_reference in the STL, is there an other straight forward way to do this?

EDIT: I clarified the example a bit

解决方案

Something along the lines of:

template< typename T > struct UnwrapReference; template< typename T > struct UnwrapReference { typedef T type; } template< > struct UnwrapReference< std::reference_wrapper< T > > { typedef T type; }

Untested though, but that's the gist of how you'd probably be able to do it.

更多推荐

如何像boost :: unwrap

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

发布评论

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

>www.elefans.com

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