具有rvalue参数调用的C ++ 11模板化函数 [英] C++11 templated function with rvalue param call
问题描述
在某些O类中,我具有模板化函数 test2
:
In some class O, I have templated function test2
:
struct A{int value;};
struct O{
A value;
template<typename Args>
static void test2(Args &&args){
std::cout << std::endl << "!!!" << std::is_rvalue_reference<decltype(args)>::value << std::endl;
}
};
然后,我想从另一个函数中调用此函数:
Than, I want to call this function from another one:
template<typename Args>
void test(Args &&args){
using t = decltype(std::forward<Args>(args).value);
std::cout << std::is_rvalue_reference<decltype(args)>::value;
std::cout << std::is_rvalue_reference<decltype(std::forward<Args>(args).value)>::value;
std::cout << std::is_rvalue_reference<t>::value;
// All ok
O.test2(std::forward<Args>(args).value);
// Alvays rvalue, even if agrs is lvalue
O::template test2<t>(
std::forward<t>(
std::forward<Args>(args).value
)
);
// Nor work at all, cant cast A to A&&
O::template test2<t>(
std::forward<Args>(args).value
);
);
}
http://coliru.stacked-crooked.com/a/3bbf040904845a54
如果我只是通过 std :: forward< Args>(args).value
而不指定模板类型,它会正确推断出类型,但是如果我必须通过类型,该怎么办?通话功能比?
If I just pass std::forward<Args>(args).value
without specifying template type, it deduce type correctly, but what if I have to pass type, how should I call function than?
看来我不能手动推断类型正确。
It seems I can't manually deduce type correctly.
更新
我需要明确指定参数,因为我具有这样的功能(伪代码):
I need to specify arguments explicitly, because I have function like this (pseudo-code):
//initially call wind from somewhere.
// Tuple defined in class and is std::tuple
template<class Callback, class ...Args>
void wind(Tuple&& tuple, Callback &&callback){
using elementT = decltype(std::get<index>(std::forward<Tuple>(tuple)));
///
/// !!! Problem here !!!
///
callback.template operator()<elementT, Args...>( std::get<index>(std::forward<Tuple>(tuple)) ); // std::get automatically return &/&&
// recursivly call wind until the end
wind<Callback, Args...>( std::forward<Tuple>(tuple), std::forward<Callback>(callback));
}
// callback looks like:
struct CallMe{
// Args provide type info. No function arguments here.
template<class Data, class ...Args>
void operator(Data &&data){
}
}
这个问题与调用和wind()函数有关。
This question related to call both - wind and callback() functions.
推荐答案
基于这样的知识,在右值引用的对象内部的对象也被右值引用了。
我决定以这个结尾(灵感来自 https://stackoverflow.com/a/24083200/1559666):
template<class T, class FieldT>
using addRefU = typename std::conditional<
std::is_rvalue_reference<T>::value,
typename std::add_rvalue_reference< FieldT >::type,
typename std::conditional<
std::is_rvalue_reference<FieldT>::value,
typename std::add_rvalue_reference< FieldT >::type,
typename std::add_lvalue_reference< FieldT >::type
>::type
>::type;
T T::value(FieldT) resolve
------------------------------------------------------------
rvalue lvalue rvalue
rvalue rvalue rvalue
lvalue rvalue rvalue
lvalue lvalue lvalue
using t = addRefU<decltype(args), decltype(args.value)>;
O::template test2<t>(
static_cast<t>(args.value)
);
http://coliru.stacked-crooked.com/a/40d10f5a2f45c288
对我来说足够简短。
PS如果有人对此有一些预防措施,我很乐意听听他们的意见。
这篇关于具有rvalue参数调用的C ++ 11模板化函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!