C++ 如何使用谓词获取元组的元素引用?

C++ 如何使用谓词获取元组的元素引用?,c++,templates,tuples,constexpr,C++,Templates,Tuples,Constexpr,这是我在Stackoverflow上的第一篇文章 我现在遇到了一个问题。我有一个在类(或结构)之外工作的代码,但当我放入一个类时,它失败了 我的目标是能够使用谓词获取元组元素的引用。 我在这里找到了一个查找tuple的find_if的例子 它工作得很好 所以我得到了元素所在的元组索引 现在我可以很容易地使用std::get获取我的参考资料 我的示例在main.cpp中工作,因此我决定将其移动到我的类中,以获得以下形式的api: constexpr T&MyStruct::get(const ch

这是我在Stackoverflow上的第一篇文章

我现在遇到了一个问题。我有一个在类(或结构)之外工作的代码,但当我放入一个类时,它失败了

我的目标是能够使用谓词获取元组元素的引用。

我在这里找到了一个查找tuple的find_if的例子

它工作得很好

所以我得到了元素所在的元组索引

现在我可以很容易地使用
std::get
获取我的参考资料

我的示例在main.cpp中工作,因此我决定将其移动到我的类中,以获得以下形式的api:

constexpr T&MyStruct::get(const char*name)

但是现在我的代码没有编译。 注释代码未编译:

#包括
#包括
/******************************************************************************************************/
//查找元组元素索引的助手
模板
constepr F for_each_impl(元组和t、F和F、std::index_序列)
{
return(void)std::initializer_list{(std::forward(f)(std::get(std::forward(t))),0)…},f;
}
模板
每个元素的常量表达式F(元组和t、F和F)
{
返回每一个impl(std::forward(t),std::forward(f),
std::make_index_sequence{});
}
模板
constexpr size\u t find\u if(元组和元组,谓词pred)
{
size\u t index=std::tuple\u size::value;
大小\u t currentIndex=0;
bool-found=false;
对于每个(元组,[&](自动和值)
{
if(!found&&pred(value))
{
指数=当前指数;
发现=真;
}
++电流指数;
});
收益指数;
}
/***********************************************************************************************/
模板
结构标签类型{
constexpr LabelType(const char*label,T val):_label(label),_value(val){}
常量字符*_标签;
T_值;
};
模板
结构标签{
constexpr LabelList(Args…Args):\元组(std::forward(Args)…{}
//constexpr自动获取(const char*name)const{
//constexpr auto index=find_if(_tuple,[&](const auto&type){
//返回类型。\u label==名称;
//        });
//返回std::get(_tuple);
//}
std::tuple _tuple;
};
int main(){
constexpr自动列表=标签列表{
标签类型{“int-label”,123},
标签类型{“字符串标签”,“你好世界”},
标签类型{“浮动标签”,3.14f},
标签类型{“bool label”,false},
};
constexpr auto index=find_if(list._tuple,[&](const auto&type){
返回类型。_label==“字符串标签”;
});
constexpr auto&string\u ref=std::get(list.\u tuple);
标准::cout
#include <tuple>
#include <iostream>

/******************************************************************************************************/
//Helper to find tuple element index


template <class Tuple, class F, std::size_t... I>
constexpr F for_each_impl(Tuple&& t, F&& f, std::index_sequence<I...>)
{
    return (void)std::initializer_list<int>{(std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))), 0)...}, f;
}

template <class Tuple, class F>
constexpr F for_each(Tuple&& t, F&& f)
{
    return for_each_impl(std::forward<Tuple>(t), std::forward<F>(f),
        std::make_index_sequence<std::tuple_size<std::remove_reference_t<Tuple>>::value>{});
}

template<typename Tuple, typename Predicate>
constexpr size_t find_if(Tuple&& tuple, Predicate pred)
{
    size_t index = std::tuple_size<std::remove_reference_t<Tuple>>::value;
    size_t currentIndex = 0;
    bool found = false;
    for_each(tuple, [&](auto&& value)
        {
            if (!found && pred(value))
            {
                index = currentIndex;
                found = true;
            }
            ++currentIndex;
        });
    return index;
}
/***********************************************************************************************/

template <typename T>
struct LabelType {

    constexpr LabelType(const char* label, T val) : _label(label), _value(val) {}

    const char* _label;
    T _value;
};

template <typename ...Args>
struct LabelList {

    constexpr LabelList(Args... args) : _tuple(std::forward<Args>(args)...) {}

    //constexpr auto& get(const char* name) const {
    //    constexpr auto index = find_if(_tuple, [&](const auto& type) {
    //        return type._label == name;
    //        });

    //    return std::get<std::tuple_element_t<index, std::tuple<Args...>>>(_tuple);
    //}

    std::tuple<Args...> _tuple;
};



int main() {
    constexpr auto list = LabelList{
        LabelType{"int label", 123},
        LabelType{"string label", "hello world"},
        LabelType{"float label", 3.14f},
        LabelType{"bool label", false},
    };

    constexpr auto index = find_if(list._tuple, [&]( const auto& type) {
        return type._label == "string label";
        });

    constexpr auto& string_ref = std::get<std::tuple_element_t<index, decltype(list._tuple)>>(list._tuple);

    std::cout << string_ref._label << " - " << string_ref._value;


    //constexpr auto& string_ref2 = list.get("string label");

    //std::cout << string_ref2._label << " - " << string_ref2._value;
}