Here's a modern C++ solution based on the answer by john and comment by Sjoerd:
#include <ostream>
#include <streambuf>
// Adapted from https://stackoverflow.com/a/11826666
class oblivion_stream final : public std::ostream {
public:
oblivion_stream() noexcept : std::ostream(&os_buffer_)
{
}
private:
class oblivion_stream_buffer_ final : public std::streambuf {
protected:
[[nodiscard]] auto overflow(int_type ch) noexcept -> int_type override
{
return ch;
}
};
oblivion_stream_buffer_ os_buffer_{};
};