From e12e73e6704c734068bdbb0205d2642f1a6af281 Mon Sep 17 00:00:00 2001 From: Jacob Taylor Hindle Date: Tue, 13 Jun 2017 15:59:21 +0100 Subject: [PATCH] ES.34 Add alternative example. --- CppCoreGuidelines.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index c563040..21aef97 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -10675,6 +10675,32 @@ Requires messy cast-and-macro-laden code to get working right. } **Alternative**: Overloading. Templates. Variadic templates. + #include + + void error(int severity) + { + std::cerr << std::endl; + std::exit(severity); + } + + template + constexpr void error(int severity, T head, Ts... tail) + { + std::cerr << head; + error(severity, tail...); + } + + void use() + { + error(7); // No crash! + error(5, "this", "is", "not", "an", "error"); // No crash! + + std::string an = "an"; + error(7, "this", "is", "not", an, "error"); // No crash! + + error(5, "oh", "no", nullptr); // Compile error! No need for nullptr. + } + ##### Note