Created
October 13, 2017 13:57
-
-
Save gatchamix/bc1c4077730977a86cddfe88aa2d63a9 to your computer and use it in GitHub Desktop.
helper macro to convert applicable data into an auto_list equivalent
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
#include <utility> | |
#include <iterator> | |
#include <array> | |
#include <string_view> | |
// | |
template <auto...> | |
struct auto_list | |
{}; | |
// | |
namespace detail | |
{ | |
template <auto S, auto... Is> | |
auto constexpr to_list(std::index_sequence<Is...>) | |
{ return auto_list<(*S)[Is]...>{}; } | |
auto constexpr size(char const *const str) | |
{ | |
auto i = std::size_t{}; | |
while (str[i] != '\0') ++i; | |
return i; | |
} | |
} | |
#define TO_LIST(var) \ | |
[&]() \ | |
{ \ | |
using detail::size; \ | |
using std::size; \ | |
static auto constexpr tmp = var; \ | |
return detail::to_list<&tmp>(std::make_index_sequence<size(var)>{}); \ | |
}(); | |
// | |
int main() | |
{ | |
auto list1 = TO_LIST("abc"); | |
static_assert(list1); | |
auto constexpr str = "def"; | |
auto list2 = TO_LIST(str); | |
static_assert(list2); | |
auto constexpr arr = std::array{ 1, 2, 3 }; | |
auto list3 = TO_LIST(arr); | |
static_assert(list3); | |
auto constexpr view = std::string_view{ "456" }; | |
auto list4 = TO_LIST(view); | |
static_assert(list4); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment