Skip to content

Instantly share code, notes, and snippets.

@falemagn
Last active June 27, 2019 12:50
Show Gist options
  • Save falemagn/d446ee38740fc8f19a44b0184a50bd7b to your computer and use it in GitHub Desktop.
Save falemagn/d446ee38740fc8f19a44b0184a50bd7b to your computer and use it in GitHub Desktop.
Statically error out on a given date.
namespace error_on_date {
inline constexpr bool strequal(char const *a, char const *b) {
return *a == *b && (*a == '\0' || strequal(a + 1, b + 1));
}
}
#if defined(ERROR_ON_DATE$$USE_CLASS) && ERROR_ON_DATE$$USE_CLASS
namespace error_on_date {
struct date {
constexpr date(unsigned int year, unsigned int month, unsigned int day):
array{
months[month-1][0], months[month-1][1], months[month-1][2],
' ',
static_cast<char>(day < 10 ? ' ' : day/10 + '0'), static_cast<char>(day%10 + '0'),
' ',
static_cast<char>(year/1000 + '0'), static_cast<char>(year/100%10 + '0'), static_cast<char>(year/10%10 + '0'), static_cast<char>(year%10 + '0'),
'\0'
}
{}
constexpr bool is_today() const noexcept {
return strequal(__DATE__, array);
}
private:
const char array[12];
static constexpr const char months[12][3] = {
{ 'J', 'a', 'n' },
{ 'F', 'e', 'b' },
{ 'M', 'a', 'r' },
{ 'A', 'p', 'r' },
{ 'M', 'a', 'y' },
{ 'J', 'u', 'n' },
{ 'J', 'u', 'l' },
{ 'A', 'u', 'g' },
{ 'S', 'e', 'p' },
{ 'O', 'c', 't' },
{ 'N', 'o', 'v' },
{ 'D', 'e', 'c' },
};
};
}
# define ERROR_ON_DATE(year, month, day, msg) static_assert(!error_on_date::date(year, month, day).is_today(), msg)
#else
# define ERROR_ON_DATE$$MONTH_1 "Jan"
# define ERROR_ON_DATE$$MONTH_2 "Feb"
# define ERROR_ON_DATE$$MONTH_3 "Mar"
# define ERROR_ON_DATE$$MONTH_4 "Apr"
# define ERROR_ON_DATE$$MONTH_5 "May"
# define ERROR_ON_DATE$$MONTH_6 "Jun"
# define ERROR_ON_DATE$$MONTH_7 "Jul"
# define ERROR_ON_DATE$$MONTH_8 "Aug"
# define ERROR_ON_DATE$$MONTH_9 "Sep"
# define ERROR_ON_DATE$$MONTH_10 "Oct"
# define ERROR_ON_DATE$$MONTH_11 "Nov"
# define ERROR_ON_DATE$$MONTH_12 "Dec"
# define ERROR_ON_DATE$$DAY_1 " 1"
# define ERROR_ON_DATE$$DAY_2 " 2"
# define ERROR_ON_DATE$$DAY_3 " 3"
# define ERROR_ON_DATE$$DAY_4 " 4"
# define ERROR_ON_DATE$$DAY_5 " 5"
# define ERROR_ON_DATE$$DAY_6 " 6"
# define ERROR_ON_DATE$$DAY_7 " 7"
# define ERROR_ON_DATE$$DAY_8 " 8"
# define ERROR_ON_DATE$$DAY_9 " 9"
# define ERROR_ON_DATE$$DAY_10 "10"
# define ERROR_ON_DATE$$DAY_11 "11"
# define ERROR_ON_DATE$$DAY_12 "12"
# define ERROR_ON_DATE$$DAY_13 "13"
# define ERROR_ON_DATE$$DAY_14 "14"
# define ERROR_ON_DATE$$DAY_15 "15"
# define ERROR_ON_DATE$$DAY_16 "16"
# define ERROR_ON_DATE$$DAY_17 "17"
# define ERROR_ON_DATE$$DAY_18 "18"
# define ERROR_ON_DATE$$DAY_19 "19"
# define ERROR_ON_DATE$$DAY_20 "20"
# define ERROR_ON_DATE$$DAY_21 "21"
# define ERROR_ON_DATE$$DAY_22 "22"
# define ERROR_ON_DATE$$DAY_23 "23"
# define ERROR_ON_DATE$$DAY_24 "24"
# define ERROR_ON_DATE$$DAY_25 "25"
# define ERROR_ON_DATE$$DAY_26 "26"
# define ERROR_ON_DATE$$DAY_27 "27"
# define ERROR_ON_DATE$$DAY_28 "28"
# define ERROR_ON_DATE$$DAY_29 "29"
# define ERROR_ON_DATE$$DAY_30 "30"
# define ERROR_ON_DATE$$DAY_31 "31"
# define ERROR_ON_DATE$$DATE(year, month, day) ERROR_ON_DATE$$MONTH_##month " " ERROR_ON_DATE$$DAY_##day " " #year
# define ERROR_ON_DATE(year, month, day, msg) static_assert(!error_on_date::strequal(__DATE__, ERROR_ON_DATE$$DATE(year, month, day)), msg)
#endif
#include "error_on_date.hpp"
ERROR_ON_DATE(2019, 6, 26, "Dude, check your code!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment