I am writing a small matrix library in C++ for matrix operations. However my compiler complains, where before it did not. This code was left on a shelf for 6 months and in b
In C++14 you can use the following template to print any object which has a T::print(std::ostream&)const; member.
template
auto operator<<(std::ostream& os, T const & t) -> decltype(t.print(os), os)
{
t.print(os);
return os;
}
In C++20 Concepts can be used.
template
concept Printable = requires(std::ostream& os, T const & t) {
{ t.print(os) };
};
template
std::ostream& operator<<(std::ostream& os, const T& t) {
t.print(os);
return os;
}