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

source code of boost/libs/lexical_cast/example/small_examples.cpp