MakeString.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <ostream>
7 #include <sstream>
8 #include <string>
9 
10 /*!
11  * \ingroup UtilitiesGroup
12  * \brief Make a string by streaming into object.
13  *
14  * \snippet Test_MakeString.cpp make_string
15  */
16 class MakeString {
17  public:
18  MakeString() = default;
19  MakeString(const MakeString&) = delete;
20  MakeString& operator=(const MakeString&) = delete;
21  MakeString(MakeString&&) = default;
22  MakeString& operator=(MakeString&&) = delete;
23  ~MakeString() = default;
24 
25  // NOLINTNEXTLINE(google-explicit-constructor)
26  operator std::string() const noexcept { return stream_.str(); }
27 
28  template <class T>
29  friend MakeString operator<<(MakeString&& ms, const T& t) noexcept {
30  // clang-tidy: can get unintentional pointer casts
31  ms.stream_ << t; // NOLINT
32  return std::move(ms);
33  }
34 
35  template <class T>
36  friend MakeString& operator<<(MakeString& ms, const T& t) noexcept {
37  // clang-tidy: can get unintentional pointer casts
38  ms.stream_ << t; // NOLINT
39  return ms;
40  }
41 
42  private:
43  std::stringstream stream_{};
44 };
45 
46 inline std::ostream& operator<<(std::ostream& os,
47  const MakeString& t) noexcept {
48  return os << std::string{t};
49 }
Make a string by streaming into object.
Definition: MakeString.hpp:16