完美转发仅仅发生于当调用该函数时会发生类型推倒
如
template<typename T>
void test(T&&a){
}
如下情况CallService并不会发生完美转发
template<typename R,typename ...Paras>
class ServiceClient
{
ReturnType<R> _returnVal;
ParameterType<Paras...>_parameters;
std::string _serviceName;
public:
//this not a universival reference because this Paras is not dedeced in this function
//but deduced in the class
R CallService(const Paras&... p){
Serializer Is;
_parameters.serialize(&Is, p...);
std::string s(Is.data(),Is.size());
std::tuple<Paras...> items;
int a;
using args_type = std::tuple<typename std::decay<Paras>::type...>;
Serializer ds(StreamBuffer(s.c_str(), s.size()));
constexpr auto N = std::tuple_size<typename std::decay<args_type>::type>::value;
args_type args = ds.get_tuple < args_type > (std::make_index_sequence<N>{});
++a;
//get the serize parameters use _parameters
//send to server
//get the replay string
//use _returnval to un serlize the parameters from replay string
//then get the ret val
}
R ServiceFunc(Paras&& ...p){
}
ServiceClient(std::string name){
_serviceName = name;
//get the service signature
}
};
原因是CallService的函数参数类型在定义 ServiceClient时就已经确定,此时在调用CallService不会发生类型推导。