const char str1[] = "Anna"; const char str2[] = "Denis"; const char str3[] = "Vladimir"; const char str4[] = "Alexey"; const char *arr[] = { str1, str2, str3, str4 };
struct str_1 { static const int size = sizeof(str1); }; struct str_2 { static const int size = sizeof(str2); }; struct str_3 { static const int size = sizeof(str3); }; struct str_4 { static const int size = sizeof(str4); }; typedef LOKI_TYPELIST_4(str_1, str_2, str_3, str_4) List;
struct str_1 { static const int size = sizeof(str1); }; struct str_2 { static const int size = sizeof(str2); }; struct str_3 { static const int size = sizeof(str3); }; struct str_4 { static const int size = sizeof(str4); }; typedef LOKI_TYPELIST_4(str_1, str_2, str_3, str_4) List; #define GetMaxLen(TypeList) \ \ template<class Cur_Type> \ struct len \ { \ static const int cur_size = Cur_Type::Head::size; \ static const int next_size = len<Cur_Type::Tail>::max_size; \ static const int max_size = cur_size > next_size ? cur_size : next_size ; \ }; \ \ template<> \ struct len<NullType> \ { \ static const int max_size = 0; \ }; \ \ static const int ml = len<TypeList>::max_size; \ GetMaxLen(List); // *.cpp // LOKI_STATIC_CHECK((ml == sizeof(str3)), size_is_wrong);
constexpr const char str1[] = "Anna"; constexpr const char str2[] = "Denis"; constexpr const char str3[] = "Vladimir"; constexpr const char str4[] = "Alexey"; constexpr const char *arr[] = { str1, str2, str3, str4 }; #define GetMaxLenght(array) \ constexpr unsigned char str_len(const char* const str) \ {\ return *str ? (1 + str_len(str + 1)) : 0;\ }\ \ template <int index> \ struct MaxLenght\ {\ static const int prev_size = MaxLenght<index-1>::max_size;\ static const int cur_size = str_len(array[index]);\ static const int max_size = cur_size > prev_size ? cur_size : prev_size;\ };\ \ template <>\ struct MaxLenght<-1>\ {\ static const int max_size = 0;\ };\ static const int AmountStr = sizeof(array) / sizeof(array[0]);\ static const int array##_max_size = MaxLenght<AmountStr-1>::max_size; GetMaxLenght(arr); // *.cpp // static_assert((arr_max_size == 8), "Error");
Source: https://habr.com/ru/post/192736/
All Articles