-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstexpr_string.hpp
More file actions
56 lines (43 loc) · 1.34 KB
/
Constexpr_string.hpp
File metadata and controls
56 lines (43 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// Created by centimo on 17.04.19.
//
#ifndef TEST_CONSTEXPR_STRING_HPP
#define TEST_CONSTEXPR_STRING_HPP
#include <utility>
constexpr size_t length(const char* str)
{
return (*str == 0) ? 0 : length(str + 1) + 1;
}
template <char... symbols>
struct Constexpr_string
{
static constexpr char value[sizeof... (symbols)] = {symbols...};
static constexpr size_t size = sizeof... (symbols);
using Char_sequence = std::integer_sequence<char, symbols...>;
};
// based on GCC extension, NOT STANDARD
template <typename Char_type = char, Char_type... symbols>
inline constexpr Constexpr_string<symbols...> operator ""_const()
{
return Constexpr_string<symbols...>();
}
// Standard-correct variant
template <typename Lambda_type, std::size_t... indexes>
constexpr static auto get_constexpr_string(Lambda_type function, std::index_sequence<indexes...>)
{
return Constexpr_string<function()[indexes]...>();
}
template <std::size_t N,
typename Lambda_type,
typename Indices = std::make_index_sequence<N>
>
constexpr static auto get_constexpr_string(Lambda_type function)
{
return get_constexpr_string(function, Indices{});
}
template <typename Lambda_type>
static constexpr auto get_constexpr_string(Lambda_type function)
{
return get_constexpr_string<length(function())>(function);
}
#endif //TEST_CONSTEXPR_STRING_HPP