Still trying to learn how to use templates, concepts, constraints.
I wanted to use std::ranges::copy() to print out the contents of a std::map and I saw this answer. Impressed, I wonder if I could constrain the pair_adaptor to only work on std::pairs where the elements were printable.
So, I wrote this:
template <class T>
concept PrintablePair = requires(std::ostream & os, T a)
{
os << a.first;
os << a.second;
};
template <PrintablePair pair_type>
class pair_adaptor
{
public:
const pair_type& m;
pair_adaptor(const pair_type& a) : m(a) {}
friend std::ostream& operator << (std::ostream& out,
const pair_adaptor <pair_type>& d)
{
const pair_type& m = d.m;
return out << m.first << " => " << m.second;
}
};
This works well for maps that look like this:
std::map<int,int> m1;
std::ranges::copy(m1,
std::ostream_iterator<
pair_adaptor<decltype(*m1.begin())> >(std::cout, "\n"));
Now, I would like to expand this further so that I can print maps that look like this:
std::map<int, std::pair<int, int>>
So, I think that I need to be able to recursively print pairs, but I am lost.