C++學習筆記 引用摺疊與完美轉發
阿新 • • 發佈:2018-11-07
完美轉發僅僅發生於當呼叫該函式時會發生型別推倒
如
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不會發生型別推導。