Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define assert(_)
- namespace helper {
- template<char...> struct toInt;
- template<> struct toInt<> { enum {value = 0, log=1, valid = true}; };
- template<char h, char... t> struct toInt<h, t...> {
- typedef toInt<t...> tail;
- enum { i = h-'0',
- log = tail::log*10,
- value = i*tail::log + tail::value,
- valid = tail::valid && (unsigned(i) < 10)
- };
- };
- }
- class month_t {
- int m;
- month_t(int m) : m{m} { }
- public:
- operator int() { return m; }
- static month_t fromInt(int m) { assert(m >= 1 && m <= 12); return {m}; }
- };
- class day_t {
- int d;
- day_t(int d) : d{d} { }
- public:
- operator int() { return d; }
- static day_t fromInt(int d) { assert(d >= 1 && d <= 31); return {d}; }
- };
- template <char... a> month_t operator "" _month() {
- enum { m = helper::toInt<a...>::value };
- static_assert(helper::toInt<a...>::valid && m >= 1 && m <= 12, "Invalid month");
- return month_t::fromInt(m);
- };
- template <char... a> day_t operator "" _day() {
- enum { m = helper::toInt<a...>::value };
- static_assert(helper::toInt<a...>::valid && m >= 1 && m <= 31, "Invalid day");
- return day_t::fromInt(m);
- };
- #include <iostream>
- void setBirthDay( day_t d, month_t m) {
- std::cout << d << "/" << m << std::endl;
- }
- int main() {
- setBirthDay( 9_day , 7_month );
- // setBirthDay( 39_day , 7_month ); //error: static_assert failed "Invalid day"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement