]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/lexical_cast/example/small_examples.cpp
1 // Copyright 2013 Antony Polukhin
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/lexical_cast.hpp>
11 //[lexical_cast_log_errno
12 //`The following example uses numeric data in a string expression:
14 void log_message(const std::string
&);
16 void log_errno(int yoko
)
18 log_message("Error " + boost::lexical_cast
<std::string
>(yoko
) + ": " + strerror(yoko
));
21 //] [/lexical_cast_log_errno]
24 //[lexical_cast_fixed_buffer
25 //`The following example converts some number and puts it to file:
27 void number_to_file(int number
, FILE* file
)
29 typedef boost::array
<char, 50> buf_t
; // You can use std::array if your compiler supports it
30 buf_t buffer
= boost::lexical_cast
<buf_t
>(number
); // No dynamic memory allocation
31 std::fputs(buffer
.begin(), file
);
34 //] [/lexical_cast_fixed_buffer]
36 //[lexical_cast_substring_conversion
37 //`The following example takes part of the string and converts it to `int`:
39 int convert_strings_part(const std::string
& s
, std::size_t pos
, std::size_t n
)
41 return boost::lexical_cast
<int>(s
.data() + pos
, n
);
44 //] [/lexical_cast_substring_conversion]
46 void log_message(const std::string
&) {}