-
-
Notifications
You must be signed in to change notification settings - Fork 617
Expand file tree
/
Copy pathcopyfmt.cpp
More file actions
33 lines (27 loc) · 724 Bytes
/
copyfmt.cpp
File metadata and controls
33 lines (27 loc) · 724 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// https://cirosantilli.com/linux-kernel-module-cheat#cpp
#include <cassert>
#include <iomanip>
#include <iostream>
#include <sstream>
int main() {
constexpr float pi = 3.14159265359;
std::stringstream ss;
// Sanity check default print.
ss << pi;
assert(ss.str() == "3.14159");
ss.str("");
// Change precision format to scientific,
// and restore default afterwards.
std::ios ss_state(nullptr);
ss_state.copyfmt(ss);
ss << std::setprecision(2);
ss << std::scientific;
ss << pi;
assert(ss.str() == "3.14e+00");
ss.str("");
ss.copyfmt(ss_state);
// Check that cout state was restored.
ss << pi;
assert(ss.str() == "3.14159");
ss.str("");
}