I am trying to get a boost::hana::string
by order as inserted in tuple looking by std::string_view
name in an array (please check code, it is easier to understand there than in my words).
I have got it, but the syntax really gets messy and lost its self-explanatory meaning, because it is necessery to call two functions (1 fn for getting the index, and 1 templ. fn por getting the string) insteas of only one direct call.
Please, be aware that the problem is harder than it looks, due to the fact that boost::hana::string
returns a diferent type even if you change a single char in the string, regardless their lengths.
The source code is also available in coliru: http://coliru.stacked-crooked.com/a/45ea8db0d6b4dbad
#include <array> #include <tuple> #include <iostream> #include <string_view> #include <boost/hana/string.hpp> using namespace std::string_view_literals; constexpr std::array images = { "USERS.PNG"sv, "LESSONS.PNG"sv, "COURSES.PNG"sv, "ALUMNS.PNG"sv }; constexpr std::tuple sources = { BOOST_HANA_STRING("1"), BOOST_HANA_STRING("2"), BOOST_HANA_STRING("3"), BOOST_HANA_STRING("4") }; constexpr size_t image(const std::string_view& name) { size_t i = 0; for (auto& image : images) if (image == name || !++i) break; return i; } template <size_t I> constexpr auto source() { return std::get<I>(sources); } //constexpr auto combined(const std::string_view& name) //{ // constexpr auto index = image(name); // return source<index>(); //} int main() { //constexpr auto hana_str = combined("LESSONS.PNG"sv); constexpr auto index = image("LESSONS.PNG"sv); constexpr auto hana_string = source<index>(); static_assert(std::string_view(hana_string.c_str()) == "2"sv); }
The function I would like to have is the commented as "combined" in above code.
https://stackoverflow.com/questions/67013412/2-x-1-in-functions-and-templates April 09, 2021 at 08:11AM
没有评论:
发表评论