1 | #include <string>
|
---|
2 | #include <iostream>
|
---|
3 | #include <boost/spirit/include/karma.hpp>
|
---|
4 |
|
---|
5 | /** Custom policy for floating point number generators */
|
---|
6 | template<typename T>
|
---|
7 | struct real_policy : boost::spirit::karma::real_policies<T>
|
---|
8 | {
|
---|
9 | static int floatfield(T)
|
---|
10 | {
|
---|
11 | return boost::spirit::karma::real_policies<T>::fmtflags::fixed;
|
---|
12 | }
|
---|
13 |
|
---|
14 | static unsigned int precision(T)
|
---|
15 | {
|
---|
16 | return std::numeric_limits<T>::digits10 + 1;
|
---|
17 | }
|
---|
18 |
|
---|
19 | };
|
---|
20 |
|
---|
21 | void write_double(std::back_insert_iterator<std::string> &sink, double &value)
|
---|
22 | {
|
---|
23 | boost::spirit::karma::real_generator<double, real_policy<double> > generator;
|
---|
24 | boost::spirit::karma::generate(sink, generator, value);
|
---|
25 | }
|
---|
26 |
|
---|
27 | int main()
|
---|
28 | {
|
---|
29 | std::string s;
|
---|
30 | s.reserve(100);
|
---|
31 | std::back_insert_iterator<std::string> sink(s);
|
---|
32 |
|
---|
33 | double value = 0.09999999999999987;
|
---|
34 | write_double(sink, value);
|
---|
35 | std::cout << s << " != " << value << std::endl;
|
---|
36 | return 0;
|
---|
37 | }
|
---|