Created
November 4, 2021 06:24
-
-
Save bolry/c28bdb108926b9f3040f60faf0837e47 to your computer and use it in GitHub Desktop.
Avoiding macros for your printing example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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