本文介绍了如何像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_reference一样解开std :: reference_wrapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!