I use std::reference_wrapperfor assembly type ( double) and for user-defined type ( std::string).
Why do they behave differently with the flow operator?
#include<functional> //reference wrapper
#include<iostream>
void fd(double& d){}
void fs(std::string& s){}
int main(){
double D = 5.;
std::reference_wrapper<double> DR(D);
std::cout << "DR = " << DR << std::endl;
fd(DR);
std::string S = "hello";
std::reference_wrapper<std::string> SR(S);
std::cout << "SR = " << static_cast<std::string&>(SR) << std::endl;
std::cout << "SR = " << SR << std::endl;
fs(SR);
}
http://coliru.stacked-crooked.com/a/fc4c614d6b7da690
Why in the first case, DR is converted to double and printed, and in the second - not? Is there any work around?
Ok, now I see that in the case of Ostream, I tried to call a template function that is not allowed:
#include<functional> //reference wrapper
void double_fun(double const& t){};
template<class C>
void string_fun(std::basic_string<C> const& t){};
int main(){
double D = 5.;
std::reference_wrapper<double> DR(D);
double_fun(DR);
std::string S = "hello";
std::reference_wrapper<std::string> SR(S);
string_fun(SR);
string_fun(SR.get());
string_fun(static_cast<std::string&>(SR));
string_fun(*&SR);
}
source
share