There are many ways to implement this. The only thing I prefer is to provide a free ADL to_string function and operator overloading <
Namespace added to spell out ADL:
#include <string>
#include <ostream>
#include <sstream>
#include <iostream>
namespace movie
{
struct movies_t {
std::string title;
int year;
};
std::ostream& operator<<(std::ostream& os, movies_t const& arg)
{
os << "title = " << arg.title << ", year = " << arg.year;
}
std::string to_string(movies_t const& arg)
{
std::ostringstream ss;
ss << arg;
return std::move(ss).str();
}
}
int main()
{
auto m = movie::movies_t { "Star Wars", 1977 };
std::cout << m << '\n';
using std::to_string;
std::cout << to_string(m) << '\n';
}