]>
Commit | Line | Data |
---|---|---|
b32b8144 | 1 | // Copyright 2013-2017 Antony Polukhin |
7c673cae FG |
2 | |
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>.) | |
6 | ||
7 | #include <boost/lexical_cast.hpp> | |
8 | #include <string> | |
9 | #include <cstdio> | |
10 | ||
b32b8144 FG |
11 | #ifdef BOOST_MSVC |
12 | # pragma warning(disable: 4996) // `strerror` is not safe | |
13 | #endif | |
14 | ||
7c673cae FG |
15 | //[lexical_cast_log_errno |
16 | //`The following example uses numeric data in a string expression: | |
17 | ||
18 | void log_message(const std::string &); | |
19 | ||
20 | void log_errno(int yoko) | |
21 | { | |
22 | log_message("Error " + boost::lexical_cast<std::string>(yoko) + ": " + strerror(yoko)); | |
23 | } | |
24 | ||
25 | //] [/lexical_cast_log_errno] | |
26 | ||
27 | ||
28 | //[lexical_cast_fixed_buffer | |
29 | //`The following example converts some number and puts it to file: | |
30 | ||
31 | void number_to_file(int number, FILE* file) | |
32 | { | |
33 | typedef boost::array<char, 50> buf_t; // You can use std::array if your compiler supports it | |
34 | buf_t buffer = boost::lexical_cast<buf_t>(number); // No dynamic memory allocation | |
35 | std::fputs(buffer.begin(), file); | |
36 | } | |
37 | ||
38 | //] [/lexical_cast_fixed_buffer] | |
39 | ||
40 | //[lexical_cast_substring_conversion | |
41 | //`The following example takes part of the string and converts it to `int`: | |
42 | ||
43 | int convert_strings_part(const std::string& s, std::size_t pos, std::size_t n) | |
44 | { | |
45 | return boost::lexical_cast<int>(s.data() + pos, n); | |
46 | } | |
47 | ||
48 | //] [/lexical_cast_substring_conversion] | |
49 | ||
50 | void log_message(const std::string &) {} | |
51 | ||
52 | int main() | |
53 | { | |
54 | return 0; | |
55 | } | |
56 |