I was curious, and wrote this proof of concept inspired by this code review https://codereview.stackexchange.com/q/104428/38143 post. It's probably silly hack to use std::codecvt
for this, and the code may contain bugs, so use at your own risk:
#include <locale>
#include <algorithm>
#include <iostream>
#include <iomanip>
class swallow_line_facet : public std::codecvt<char, char, std::mbstate_t> {
public:
swallow_line_facet(std::size_t ref = 0)
: std::codecvt<char, char, std::mbstate_t>(ref) {}
protected:
result do_out(
state_type &,
const intern_type* from,
const intern_type* from_end,
const intern_type*& from_next,
extern_type* to,
extern_type* to_end,
extern_type*& to_next) const override
{
if (is_done)
return std::codecvt_base::noconv;
for (; (from < from_end) && (to < to_end); from++) {
char c = *from;
if (is_done)
*to++ = c;
if (c == '\n')
is_done = true;
}
from_next = from;
to_next = to;
return from == from_end
? std::codecvt_base::ok
: std::codecvt_base::partial;
}
virtual bool do_always_noconv() const noexcept override {
return is_done;
}
private:
mutable bool is_done = false;
};
std::ostream& swallow_line(std::ostream& out)
{
out.imbue(std::locale(out.getloc(), new swallow_line_facet));
return out;
}
Demo
int main() {
/// This probably has to be called once for every program:
// http://stackoverflow.com/questions/26387054/how-can-i-use-stdimbue-to-set-the-locale-for-stdwcout
std::ios_base::sync_with_stdio(false);
std::cout << "first line" << '\n' << swallow_line;
std::cout << "second line" << '\n';
std::cout << "third line" << '\n' << swallow_line;
std::cout << "fourth line" << '\n';
std::cout << "fifth line" << '\n';
}
Output:
first line
third line
fifth line
Usage in your case:
void overridePrint(){
std::cout << "Replaced header" << std::endl;
std::cout << swallow_line;
print();
}
int main() {
/// don't forget this
std::ios_base::sync_with_stdio(false);
// ...
}