You can use a completely different approach to create debug output that does not depend on comparing whether the stream being written to is std::cout
. This approach relies on whether a flag has been turned on or not.
Create a helper namespace
The helper namespace
can hold the data and provide convenience functions to manipulate the data.
namespace Employee_NS
{
bool writeVerbose = false;
template <bool val>
std::ostream& verbose(std::ostream& outs)
{
writeVerbose = val;
return outs;
}
std::ostream& operator<<(std::ostream& outs, std::ostream& (*fun)(std::ostream&))
{
return fun(outs);
}
};
Implement Employee::output
differently
Change the return type from void
to std::ostream&
and make the function a const
member function.
std::ostream& Employee::output(std::ostream& outs) const
{
if ( Employee_NS::writeVerbose )
{
outs << "Name: " << name << std::endl;
outs << "ID number: " << id_number << std::endl;
outs << "Address: " << address << std::endl;
outs << "Salary: " << salary << std::endl;
outs << "Years worked at company: " << year_started << std::endl;
}
else {
outs << name << std::endl;
outs << id_number << std::endl;
outs << address << std::endl;
outs << salary << std::endl;
outs << year_started << std::endl;
}
return outs;
}
Add a suitable operator<<
function
Add a non-member function to be able use Employee
objects in an intuitive manner.
std::ostream& operator<<(std::ostream& out, Employee const& e)
{
return e.output(out);
}
Now, you can create verbose output regardless of whether the output goes into a file or to std::cout
.
Employee e;
// Verbose output to cout
std::cout << Employee_NS::verbose<true> << e;
std::ofstream out("test.txt");
// Verbose output to the file
out << Employee_NS::verbose<true> << e;
// Non-verbose output to the file
out << Employee_NS::verbose<false> << e;
This approach elevates the decision of whether to create verbose output to the calling function. It also provides the ability to create verbose output in any output destination.