Skip to content

Commit f3535c2

Browse files
committed
gcc<14 doesn't have <print> 🙁
1 parent 8b23899 commit f3535c2

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/main.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
#include "factorial.hpp"
22

3+
#include <%%cpp_init_replace%%/config.hpp>
34
#include <%%cpp_init_replace%%/fib.hpp>
45

5-
#include <print>
6+
#include <cstdlib>
7+
#if defined(%%CPP_INIT_REPLACE%%_COMPILER_GNU) && %%CPP_INIT_REPLACE%%_COMPILER_VERSION_MAJOR < 14
8+
# include <cstdio>
9+
# include <format>
10+
# define %%CPP_INIT_REPLACE%%_PRINTLN(fp, ...) \
11+
(void)std::fputs(std::format(__VA_ARGS__).c_str(), fp); \
12+
(void)std::fputc('\n', fp)
13+
#else
14+
# include <print>
15+
# define %%CPP_INIT_REPLACE%%_PRINTLN(...) std::println(__VA_ARGS__)
16+
#endif
617
#include <stdexcept>
718
#include <string>
8-
#include <cstdlib>
919

1020
int main(int argc, char *argv[]) {
1121
if (argc != 2) {
12-
std::println(stderr, "Wrong number of arguments, expected 1, got {}", argc - 1);
22+
%%CPP_INIT_REPLACE%%_PRINTLN(stderr, "Wrong number of arguments, expected 1, got {}", argc - 1);
1323
return 1;
1424
}
1525
auto const num = [arg = argv[1]]() {
1626
try {
1727
return std::stoll(arg);
1828
} catch (std::logic_error const &e) {
19-
std::println(stderr, "Could not parse command line argument: {}", e.what());
29+
%%CPP_INIT_REPLACE%%_PRINTLN(stderr, "Could not parse command line argument: {}", e.what());
2030
std::exit(1); // NOLINT(concurrency-mt-unsafe)
2131
}
2232
}();
2333

24-
std::println("{}! = {}", num, %%cpp_init_namespace%%::factorial(num));
25-
std::println("fib({}) = {}", num, %%cpp_init_namespace%%::fib(num));
34+
%%CPP_INIT_REPLACE%%_PRINTLN(stdout, "{}! = {}", num, %%cpp_init_namespace%%::factorial(num));
35+
%%CPP_INIT_REPLACE%%_PRINTLN(stdout, "fib({}) = {}", num, %%cpp_init_namespace%%::fib(num));
2636

2737
return 0;
2838
}

0 commit comments

Comments
 (0)