Let ss be a std::stringstream. How to specify a concept for (ss << some_type).str() is a std::string?
Here is the code I have so far:
#include <iostream>
#include <sstream>
namespace detail {
template< class T, class U >
concept SameHelper = std::is_same_v<T, U>;
}
template< class T, class U >
concept same_as = detail::SameHelper<T, U> && detail::SameHelper<U, T>;
template<typename T>
concept ssToStr = requires(T a, std::stringstream ss) {
{ (ss << a).str() } -> same_as<std::string>;
};
void call(ssToStr auto obj)
{
std::stringstream ss;
std::cout << (ss << obj).str() << std::endl;
}
int main()
{
call("Hi");
return 0;
}
You can check online that the code does not compile. The first part of the error messages reads:
<source>:25:5: error: no matching function for call to 'call'
call("Hi");
^~~~