Printing std::u8stream in std::ostream and a concept for checking it

44 Views Asked by At

In the codebase I have to support there is a concept checking that some type can be printed in output stream:

template <typename T>
concept OstreamFormattable = requires( const T& t, std::ostream& out ) {
    { out << t } -> std::convertible_to<std::ostream&>;
};

and there is also a user defined overload of operator << for std::u8string:

std::ostream & operator <<( std::ostream & s, const std::u8string & str ) {
  return s << std::string( str.begin(), str.end() );
}

And OstreamFormattable<std::u8string> always evaluates to true in Visual Studio, but in GCC/Clang it is so only if operator << is declared before the concept, and false if it is declared later. Online demo: https://godbolt.org/z/jM317xq7P

Is it a bug of Visual Studio compiler? Is it possible to fix the concept or operator<< declaration to work in all compilers independently on the order of their inclusion?

0

There are 0 best solutions below