]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/lexical_cast/example/generic_stringize.cpp
1 // Copyright Antony Polukhin, 2013-2022.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See the accompanying file LICENSE_1_0.txt
5 // or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
7 #include <boost/config.hpp>
9 # pragma warning(disable: 4512) // generic_stringize.cpp(37) : warning C4512: 'stringize_functor' : assignment operator could not be generated
12 //[lexical_cast_stringize
14 In this example we'll make a `stringize` method that accepts a sequence, converts
15 each element of the sequence into string and appends that string to the result.
17 Example is based on the example from the [@http://www.packtpub.com/boost-cplusplus-application-development-cookbook/book Boost C++ Application Development Cookbook]
18 by Antony Polukhin, ISBN 9781849514880. Russian translation: [@https://dmkpress.com/catalog/computer/programming/c/978-5-97060-868-5/ ISBN: 9785970608685].
20 Step 1: Making a functor that converts any type to a string and remembers result:
23 #include <boost/lexical_cast.hpp>
25 struct stringize_functor
{
30 explicit stringize_functor ( std :: string
& res
)
35 void operator ()( const T
& v
) const {
36 result
+= boost :: lexical_cast
< std :: string
>( v
);
40 //` Step 2: Applying `stringize_functor` to each element in sequence:
41 #include <boost/fusion/include/for_each.hpp>
42 template < class Sequence
>
43 std :: string
stringize ( const Sequence
& seq
) {
45 boost :: fusion :: for_each ( seq
, stringize_functor ( result
));
49 //` Step 3: Using the `stringize` with different types:
50 #include <boost/fusion/adapted/boost_tuple.hpp>
51 #include <boost/fusion/adapted/std_pair.hpp>
54 boost :: tuple
< char , int , char , int > decim ( '-' , 10 , 'e' , 5 );
55 if ( stringize ( decim
) != "-10e5" ) {
59 std :: pair
< int , std :: string
> value_and_type ( 270 , "Kelvin" );
60 if ( stringize ( value_and_type
) != "270Kelvin" ) {
67 //] [/lexical_cast_stringize]