small_examples.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright Antony Polukhin, 2013-2019.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See the accompanying file LICENSE_1_0.txt
  4. // or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
  5. #include <boost/lexical_cast.hpp>
  6. #include <string>
  7. #include <cstdio>
  8. #ifdef BOOST_MSVC
  9. # pragma warning(disable: 4996) // `strerror` is not safe
  10. #endif
  11. //[lexical_cast_log_errno
  12. //`The following example uses numeric data in a string expression:
  13. void log_message(const std::string &);
  14. void log_errno(int yoko)
  15. {
  16. log_message("Error " + boost::lexical_cast<std::string>(yoko) + ": " + strerror(yoko));
  17. }
  18. //] [/lexical_cast_log_errno]
  19. //[lexical_cast_fixed_buffer
  20. //`The following example converts some number and puts it to file:
  21. void number_to_file(int number, FILE* file)
  22. {
  23. typedef boost::array<char, 50> buf_t; // You can use std::array if your compiler supports it
  24. buf_t buffer = boost::lexical_cast<buf_t>(number); // No dynamic memory allocation
  25. std::fputs(buffer.begin(), file);
  26. }
  27. //] [/lexical_cast_fixed_buffer]
  28. //[lexical_cast_substring_conversion
  29. //`The following example takes part of the string and converts it to `int`:
  30. int convert_strings_part(const std::string& s, std::size_t pos, std::size_t n)
  31. {
  32. return boost::lexical_cast<int>(s.data() + pos, n);
  33. }
  34. //] [/lexical_cast_substring_conversion]
  35. void log_message(const std::string &) {}
  36. int main()
  37. {
  38. return 0;
  39. }