简体   繁体   English

仅将 2 个功能(模板化)组合在一个中

[英]Combine 2 functions (templated) in only one

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).我正在尝试按顺序获得boost::hana::string插入元组中,通过数组中的std::string_view名称查找(请检查代码,那里比我的话更容易理解)。

I have got it, but the syntax really gets messy and lost its self-explanatory meaning, because it is necessary to call two functions (1 fn for getting the index, and 1 templ. fn por getting the string) instead of only one direct call.我明白了,但是语法确实变得混乱并且失去了不言自明的含义,因为需要调用两个函数(1个fn获取索引,1个templ.fn por获取字符串)而不是直接调用一个称呼。

Please, be aware that the problem is harder than it looks, due to the fact that boost::hana::string returns a different type even if you change a single char in the string, regardless their lengths.请注意,问题比看起来更难,因为boost::hana::string会返回不同的类型,即使您更改字符串中的单个字符,无论它们的长度如何。

The source code is also available in coliru: http://coliru.stacked-crooked.com/a/45ea8db0d6b4dbad源代码也可以在 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.我想要的 function 在上面的代码中被注释为“组合”。

You can pass boost::hana::string to combined() instead in order to invoke image() at compile-time with std::string_view :您可以将boost::hana::string传递给 combine( combined() ,以便在编译时使用std::string_view调用image()

template <class CharT, CharT... s>
constexpr auto combined(const boost::hana::string<s...>& name)
{
   constexpr auto index = image(name.c_str());
   return source<index>();
}

And here's how to use it:以下是如何使用它:

using boost::hana::literals::operator""_s;
constexpr auto hana_str = combined("LESSONS.PNG"_s);
static_assert(hana_str.c_str() == "2"sv);

But in order to enable boost::hana::literals::operator""_s you need to define BOOST_HANA_CONFIG_ENABLE_STRING_UDL before including <boost/hana/string.hpp> :但是为了启用boost::hana::literals::operator""_sBOOST_HANA_CONFIG_ENABLE_STRING_UDL在包含<boost/hana/string.hpp>之前定义 BOOST_HANA_CONFIG_ENABLE_STRING_UDL :

#define BOOST_HANA_CONFIG_ENABLE_STRING_UDL
#include <boost/hana/string.hpp>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM