C++11 recursive constexpr static string length and compare
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constexpr bool is_str_end(const char *t) { | |
return !(t) || !(*t); | |
} | |
constexpr int recursive_cstr_len(const char * t) { | |
return is_str_end(t) ? | |
0 : | |
1 + recursive_cstr_len(t + 1); | |
} | |
constexpr int str_equal = 0; | |
constexpr int str_greater = 1; | |
constexpr int str_lesser = -1; | |
constexpr int recursive_cstr_cmp(const char *a, const char *b) { | |
return is_str_end(a) ? | |
( is_str_end(b) ? str_equal : str_lesser) : | |
( | |
(*a != *b) ? | |
( *a > *b ? str_greater : str_lesser) : | |
recursive_cstr_cmp(a+1,b+1) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment