Это исключительно на основе ответа 0x499602D2 и вашей ссылки на отсутствующие перегрузки 10-12 .
Я не уверен, какие функции использоватьдля проверки перегрузки 11, но 10 и 12 проверяются с std::hex
и std::endl
.
#include <iomanip>
#include <iostream>
#include <sstream>
class Foo {
private:
std::ostringstream os{};
public:
using char_type = std::ostringstream::char_type;
using traits_type = std::ostringstream::traits_type;
// generic, with perfect forwarding instead of "const T&"
template<typename T>
auto operator<<(T&& param) -> decltype(os << std::forward<T>(param), *this) {
os << std::forward<T>(param);
return *this;
}
// overload 10
Foo& operator<<(std::ios_base& (*func)(std::ios_base&)) {
func(os);
return *this;
}
// overload 11
Foo& operator<<(std::basic_ios<char_type, traits_type>& (*func)(
std::basic_ios<char_type, traits_type>&)) {
func(os);
return *this;
}
// overload 12
Foo& operator<<(std::basic_ostream<char_type, traits_type>& (*func)(
std::basic_ostream<char_type, traits_type>&)) {
func(os);
return *this;
}
auto str() { return os.str(); }
};
int main() {
Foo a;
a << "Hello Worl"; // generic
a << std::hex << 13; // 10 + generic
a << std::endl; // 12
std::cout << a.str() << "\n";
}