zeusUnnamed repository; edit this file 'description' to name the repository. |
git clone Unknown |
Log | Files | Refs |
conseval_string.cpp (2103B)
0 #include <array>
1 #include <string_view>
3 // max size object we can convert
4 static constexpr std::size_t oversized_size = 10 * 1024;
7 template<typename Value>
8 concept is_iterable = requires(const Value &value)
9 {
10 std::begin(value);
11 std::end(value);
12 };
14 template<typename Callable>
15 concept creates_iterable = requires(const Callable &callable)
16 {
17 requires is_iterable<std::decay_t<decltype(callable())>>;
18 };
21 template<typename Callable>
22 concept creates_string_like = requires(const Callable &callable)
23 {
24 requires creates_iterable<Callable>;
25 // TODO this check needs to be better
26 typename std::decay_t<decltype(callable())>::traits_type;
27 };
30 template<typename Value> struct oversized_array
31 {
32 std::array<Value, oversized_size> data{};
33 std::size_t size{};
35 using value_type = Value;
37 constexpr auto begin() const noexcept { return std::begin(data); }
38 constexpr auto end() const noexcept { return std::next(std::begin(data), static_cast<std::ptrdiff_t>(size)); }
39 };
41 template<typename Data> constexpr auto to_oversized_array(const Data &str)
42 {
43 oversized_array<typename Data::value_type> result;
44 std::copy(std::begin(str), std::end(str), std::begin(result.data));
45 result.size = str.size();
46 return result;
47 }
49 consteval auto to_right_sized_array(creates_iterable auto callable)
50 {
51 constexpr auto oversized = to_oversized_array(callable());
53 using Value_Type = typename std::decay_t<decltype(oversized)>::value_type;
54 std::array<Value_Type, oversized.size> result;
55 std::copy(std::begin(oversized), std::end(oversized), std::begin(result));
56 return result;
57 }
59 template<auto Data> inline constexpr const auto &make_static = Data;
61 consteval auto to_string_view(creates_string_like auto callable)
62 {
63 constexpr auto &static_data = make_static<to_right_sized_array(callable)>;
64 using Value_Type = typename std::decay_t<decltype(static_data)>::value_type;
65 std::basic_string_view<Value_Type> result(std::begin(static_data), std::end(static_data));
66 return result;
67 }
69 int main() {
70 auto sv = to_string_view([] -> std::string_view { return "Hello there!"; });
71 return 0;
72 }