RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 624037
Accepted
Max
Max
Asked:2020-02-06 18:58:13 +0000 UTC2020-02-06 18:58:13 +0000 UTC 2020-02-06 18:58:13 +0000 UTC

结构字段的枚举

  • 772

有结构和功能:

struct S {
    T1 t1;
    T2 t2;
    // и т.д.
} s;

template <class T> f(const T& t, const char* name) { /* что-то */ }

以下(伪)代码需要工作:

for (поле : поля(s))
    f(поле.значение, поле.имя);

也就是说,您需要以某种方式枚举结构的所有字段,将它们的类型、值和名称作为字符串获取。

我知道C++中没有反射。但是这里在执行时不需要反射,在编译或链接阶段需要反射。因此,可以通过结构声明以任何方式变态S,但不可能触及功能f。但最后,必须以某种方式创建一个结构或类似结构的东西,它允许您访问字段(s.t1等) 。

您不能使用第三方库,但可以使用宏、模板和通常所有可用的C++14功能。

c++
  • 3 3 个回答
  • 10 Views

3 个回答

  • Voted
  1. Best Answer
    user194374
    2020-02-06T20:10:42Z2020-02-06T20:10:42Z

    正如@KoVadim 在对该问题的评论中所建议的,可以使用所谓的X宏。使用它们,您的任务大致解决如下:

    #include <iostream>
    
    #define STRUCT_FIELDS \
        X(int, i) \
        X(float, f) \
        X(const char*, c)
    
    struct S {
        #define X(type, name) type name;
            STRUCT_FIELDS
        #undef X
    };
    
    template<typename T> void f(const T& t, const char* name)
    {
        std::cout << name << " = " << t << std::endl;
    }
    
    void printStruct(const S& s) {
        #define X(type, name) f(s.name, #name);
            STRUCT_FIELDS
        #undef X
    }
    
    int main(int argc, char* argv[]) {
        S s;
        s.i = 1;
        s.f = 10.1;
        s.c = "s";
    
        printStruct(s);
    }
    

    程序结果:

    i = 1
    f = 10.1
    c = s
    
    • 9
  2. gbg
    2020-02-06T19:21:26Z2020-02-06T19:21:26Z

    您可以添加我的解决方案。它旨在解析命令行选项并将它们放入一个元组中,该元组的元素可以通过名称访问。在这种情况下,编译阶段的名称将替换为元组中元素的索引。

    如何使用它:

    您需要从 opt-parser 继承该类

    #ifndef CONVOPTIONS_H
    #define CONVOPTIONS_H
    #include "opt-parser/optparser.h"
    
    struct optlist
    {
        static constexpr auto options=tuple_cat(
                    OPTPARSER::makeO<std::string>("input"),
                    OPTPARSER::makeO<std::string>("output")
                    );
    
    };
    
    class ConvOptions:public OPTPARSER::Options<optlist>
    {
    public:
        ConvOptions(int argc,char** argv);
    };
    
    #endif // CONVOPTIONS_H
    

    之后,您可以通过名称引用其字段:

    ConvOptions opts(argc,argv);
    $(opts,"input")
    
    • 3
  3. Qwertiy
    2020-02-08T06:04:03Z2020-02-08T06:04:03Z

    这是答案的外观以及获得它的步骤。
    我们仍然需要在各处添加链接并添加对字段名称的支持。

    1. http://ideone.com/6LOtIl
    2. http://ideone.com/ac32bj
    3. http://ideone.com/bG8q69
    4. http://ideone.com/umN51y
    5. http://ideone.com/bxgu9H
    6. http://ideone.com/rddXvG
    #include <typeinfo>
    #include <iostream>
    
    using namespace std;
    
    template <typename typed, int i = 0> struct enumerate
    {
        static auto get(typed x) -> decltype(x.template get<i>()) { return x.template get<i>(); };
        const static bool has_next = !is_same<decltype(typed().template get<i+1>()), void>::value;
        typedef enumerate<typed, has_next?i+1:-1> next;
    
        template <template <class> class handler> static void go(typed x)
        {
            handler<decltype(get(x))>()(get(x));
            if (has_next) next::template go<handler>(x);
        }
    };
    
    template <typename typed> struct enumerate <typed, -1>
    {
        static void get(typed x) {}
        const static bool has_next = false;
        typedef enumerate<typed, -1> next;
    
        template <template <class> class handler> static void go(typed x)
        {
        }
    };
    
    #define ENUMERABLE_FIELD(type, name, i) public: type name; \
                                            private: type get(key_t<i>) { return this->name; }
    
    struct smth
    {
        private: template <int i> struct key_t {};
        private: template <int i> void get(key_t<i>) {}
    
        ENUMERABLE_FIELD(int, x, 0)
        ENUMERABLE_FIELD(double, y, 1)
    
        public: template <int i> auto get() -> decltype(this->get(key_t<i>())) { return this->get(key_t<i>()); }
    };
    
    template <class typed> void print(const typed& t)
    {
        cout << t << endl;
    }
    
    template <class typed> struct pass_to_print
    {
        void operator () (typed t) { return print(t); }
    };
    
    int main()
    {
        smth s = {1, 2.5};
    
        enumerate<smth>::go<pass_to_print>(s);
    
        return 0;
    }
    
    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5