i think understood perfect forwarding , took code normaly illustrate it
template<typename t, typename u> std::pair<t, u> make_pair_wrapper(t&& t, u&& u) { return std::make_pair(std::forward<t>(t), std::forward<u>(u)); } int main() { std::pair<std::string, int> p1 = make_pair_wrapper("foo", 42); } but doesn't compile, saying can't convert 'std::pair<const char*, int>' 'std::pair<const char (&)[4], int>
i have admit don't understand
you forgot pass variables instead of temporaries. line incorrect
pair<string, int> p1 = make_pair_wrapper("foo",42); pair stores std::string , int, pass function const char* , int. const char* not std::string. do:
make_pair_wrapper(s, i);
or:
make_pair_wrapper(std::string("foo"), 42); you must work temporaries, can't omitted. if want have "variable" feel, can extend life of temporaries:
const int& = 42; const string& s = "foo"; then, can pass them function (but don't recommend that).
Comments
Post a Comment