I am trying to control the print outs in my program. I wrote a small script for that which takes a command line argument and decides to print or not to print depending upon the flag -v passed to it. Now I have to write out(bool) in my actual program everywhere. Is it possible to make the decision in the start of the program and just to use out instead of out(bool).
class mystreambuf : public std::streambuf { }; mystreambuf nostreambuf; std::ostream nocout(&nostreambuf); #define out(b) ((b==true)?std::cout : nocout ) int main(int argc, char* argv[]) { std::cout << "Program name is: " << argv[0] << "\n"; int counter; bool verbose; if (argc == 1) { std::cout << "No command line argument passed" << argv[0] << "\n"; } if (argc >= 2) { for (counter = 0; counter < argc; counter++) { std::cout << "Argument passed are: " << argv[counter] << "\n"; if (strcmp(argv[counter], "-v") == 0) { verbose = true; } else { verbose = false; } } } out(verbose) << "\n hello world \n"; system("pause"); }
trueandfalsein your code, they are virtually always unnecessary, and only add verbosity, which decreases readability. Don’t writeb == true, write justb. Same for your (ironically named)verboseassignment: this can and should be accomplished in a single line (instead of eight):verbose = strcmp(argv[counter], "-v") == 0;— Furthermore,outshould be a function, not a macro.