the following code:
#include <iostream>
std::ios_base &my_manip (std::basic_ios<char> &os) {
os.unsetf(std::ios_base::basefield);
os.setf(std::ios_base::scientific);
return os;
}
int main (int argc, char **argv) {
std::cout << 8.8888888 << std::endl;
std::cout << my_manip << 8.8888888 << std::endl;
return 0;
}
prints:
8.88889
18.88889
While the following code:
#include <iostream>
std::ios_base &my_manip (std::basic_ios<char> &os) {
os.unsetf(std::ios_base::basefield);
os.setf(std::ios_base::scientific);
return os;
}
int main (int argc, char **argv) {
std::cout << 8.8888888 << std::endl;
my_manip(std::cout);
std::cout << 8.8888888 << std::endl;
return 0;
}
prints the expected result:
8.88889
8.888889e+00
Can anybody tell me what is wrong with the first version?
The custom manipulator signature is not matching,
You should be doing this :
std::ostream& my_manip (std::ostream &os) {
os.unsetf(std::ios_base::basefield);
os.setf(std::ios_base::scientific);
return os;
}
std::cout << my_manip << 8.8888888 << std::endl;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With