1 | #include <boost/config/warning_disable.hpp>
|
---|
2 | #include <boost/spirit/include/karma.hpp>
|
---|
3 | #include <boost/spirit/include/karma_real.hpp>
|
---|
4 |
|
---|
5 | #include <iostream>
|
---|
6 | #include <string>
|
---|
7 | #include <iterator>
|
---|
8 |
|
---|
9 | namespace karma = boost::spirit::karma;
|
---|
10 |
|
---|
11 | // define a new real number formatting policy
|
---|
12 | template <typename Num>
|
---|
13 | struct scientific_policy : karma::real_policies<Num>
|
---|
14 | {
|
---|
15 | // we want the numbers always to be in scientific format
|
---|
16 | static int floatfield(Num n) { return std::ios_base::scientific; }
|
---|
17 | };
|
---|
18 |
|
---|
19 | int main( ) {
|
---|
20 | // define a new generator type based on the new policy
|
---|
21 | typedef
|
---|
22 | karma::real_generator<double, scientific_policy<double> > science_type;
|
---|
23 | science_type const scientific = science_type();
|
---|
24 |
|
---|
25 | std::string output;
|
---|
26 | typedef std::back_insert_iterator<std::string> output_iterator;
|
---|
27 | output_iterator sink( output );
|
---|
28 |
|
---|
29 | // should output: 1.0e-01, but will output: 10.0e-02
|
---|
30 | karma::generate(sink, scientific, 0.1);
|
---|
31 |
|
---|
32 | std::cout << output << std::endl;
|
---|
33 | };
|
---|