Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <type_traits>
- #include <iostream>
- #include <vector>
- #include <string>
- #include <iomanip>
- //it's a string view, not null terminated C str
- struct const_str{
- const char* str;
- int size;
- template<int N>
- constexpr const_str(const char (&src)[N]) : str(src), size(N - 1){} // -1, ignore the '\0'
- constexpr const_str(const char* src, int n) : str(src), size(n){}
- friend constexpr bool operator<(const_str lhs, const_str rhs){
- return (lhs.size == 0)
- ? rhs.size != 0
- : (lhs.str[0] == rhs.str[0])
- ? const_str(lhs.str + 1, lhs.size - 1) < const_str(rhs.str + 1, rhs.size - 1)
- : lhs.str[0] < rhs.str[0];
- }
- };
- std::ostream& operator<<(std::ostream& os, const_str s){
- os.write(s.str, s.size);
- return os;
- }
- constexpr const char* rfind(const_str s, char ch){
- return (s.str[s.size - 1] == ch)
- ? (s.str + s.size - 1)
- : rfind(const_str(s.str, s.size - 1), ch);
- }
- constexpr const_str substr(const_str s, const char* p, int n){
- return const_str(p, s.size - (p - s.str) - n);
- }
- constexpr const_str extract_type_id(const_str s){
- return substr(s, rfind(s, '=') + 2, 1);
- }
- template<typename T>
- constexpr const_str type_id() {
- return extract_type_id(__PRETTY_FUNCTION__);
- }
- template<typename T, typename U>
- struct type_less{
- static const bool value = type_id<T>() < type_id<U>();
- };
- //test and sample
- template<typename T, typename U>
- void print_compare(){
- auto left = type_id<T>();
- auto right = type_id<U>();
- std::cout << std::boolalpha;
- std::cout << "compare " << left << " < " << right << ":\t" << type_less<T, U>::value << std::endl;;
- }
- namespace test{
- template<typename T> struct test1{};
- }
- int main(){
- using namespace std;
- cout << type_id<test::test1<std::vector<std::string>>>() << endl;
- print_compare<int, char>();
- print_compare<char, int>();
- print_compare<int, int>();
- print_compare<int, float>();
- print_compare<float, double>();
- print_compare<std::string, double>();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement