CPP Homework 04
Exercise from OTUS C++ developer course.
helpers.h
Go to the documentation of this file.
1 #ifndef HELPERS_H
2 #define HELPERS_H
3 
4 #include <iostream>
5 #include <string>
6 #include <tuple>
7 
9 
16 template <typename T>
17 std::string to_string(T t) {
18  return std::to_string(t);
19 }
20 
27 std::string to_string(char ch) {
28  return to_string(static_cast<unsigned char>(ch));
29 }
30 
37 std::string to_string(const char *ch) {
38  return std::string(ch);
39 }
40 
47 std::string to_string(std::string const &s) {
48  return s;
49 }
50 
58 template <typename T1, typename T2>
59 void print_ip_tuple_impl(std::tuple<T1, T2> const &t, std::ostream &stream = std::cout) {
60  static_assert(std::is_same<T1, T2>());
61  stream
62  << to_string(std::get<0>(t)) << '.'
63  << to_string(std::get<1>(t)) << std::endl;
64 }
65 
72 template <typename Head, typename ... Tail>
73 void print_ip_tuple_impl(std::tuple<Head, Tail ...> const &t, std::ostream &stream = std::cout) {
74  using Second = typename std::tuple_element<0, std::tuple<Tail ...> >::type;
75  static_assert(std::is_same<Head, Second>());
76  stream << to_string(std::get<0>(t)) << '.';
77  std::apply(
78  [&stream](auto head, auto ... tail) {
79  print_ip_tuple_impl(std::make_tuple(tail...), stream);
80  }, t);
81 }
82 
84 
85 #endif
std::string to_string(T t)
Wrapper function to make string.
Definition: helpers.h:17
void print_ip_tuple_impl(std::tuple< T1, T2 > const &t, std::ostream &stream=std::cout)
Pretty print IP address.
Definition: helpers.h:59