Skip to content

Instantly share code, notes, and snippets.

@bolry
Created November 4, 2021 06:24
Show Gist options
  • Save bolry/c28bdb108926b9f3040f60faf0837e47 to your computer and use it in GitHub Desktop.
Save bolry/c28bdb108926b9f3040f60faf0837e47 to your computer and use it in GitHub Desktop.
Avoiding macros for your printing example
#include <utility>
#include <fmt/printf.h>
constexpr bool verboseMode{false};
constexpr bool logPrinting{false};
namespace my {
template<typename ... T>
inline void print(T &&... args) {
if constexpr(!verboseMode) {}
else if constexpr (logPrinting) { fmt::print(stderr, std::forward<T>(args) ...); }
else { fmt::print(std::forward<T>(args) ...); }
}
template<typename ... T>
inline auto printf(T &&... args) {
if constexpr(!verboseMode) { return; }
else if constexpr (logPrinting) { return fmt::fprintf(stderr, std::forward<T>(args) ...); }
else { return fmt::printf(std::forward<T>(args) ...); }
}
}
int main() {
my::print("print:ing NONE\n");
my::print("print:ing {}\n", "one");
my::print("print:ing {} {}\n", 2, "three");
my::printf("printf:ing NONE\n");
my::printf("printf:ing %s\n", "one");
my::printf("printf:ing %d %s\n", 2, "three");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment