ES.34 Add alternative example.

This commit is contained in:
Jacob Taylor Hindle 2017-06-13 15:59:21 +01:00
parent 03dc7d3f44
commit e12e73e670

View File

@ -10675,6 +10675,32 @@ Requires messy cast-and-macro-laden code to get working right.
} }
**Alternative**: Overloading. Templates. Variadic templates. **Alternative**: Overloading. Templates. Variadic templates.
#include <iostream>
void error(int severity)
{
std::cerr << std::endl;
std::exit(severity);
}
template <typename T, typename... Ts>
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 ##### Note