c++ - 在C++中获取结构的参数名称

标签 c++ structure

有没有什么办法可以得到C++结构体中声明的所有参数名?

struct tables
{
    struct numbers
    {
        int values;
    };

    struct people
    {
        int id;
        string firstname;
        string lastname;
    };

    struct subjects
    {
        int id;
        string name;    
        int year;
    };

    struct sale
    {
        string name;
        int size;
        bool environment;
        double v;
    };
};

例如,我想知道tables->sale 结构中的参数名称。我该怎么做?

最佳答案

对于当前的 C++,无法从语言内部确定类类型的成员。您最好的选择是创建一个合适的声明处理器,以提供合适的成员表示。例如,您可以使用 clang解析 C++ 代码并编写允许某种形式的反射的辅助声明。

C++ 的 future 版本有望包含一些反射,但 Reflection Study Group (SG7) 还没有真正开始生产任何准备好纳入 C++ 标准的东西。

关于c++ - 在C++中获取结构的参数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20337772/

相关文章:

javascript - OpenCV Rect 约定——什么是 x、y、宽度、高度?

Python/Flask Rest API 未通过带有 CORS 的预检

c - c中结构指针之间的区别

C - 当尝试更新记录时,fseek() 会影响文件的内容(结构)

c# - 如何构建 C# 解决方案

c++ - Clang 和 Intel 无法编译此 CRTP 代码

c++ - 为什么 cv::Mat::data 总是指向一个 uchar?

c++ - 在 C++ 中使用 vector 时 Push_back 导致错误

c++ - 具有静态属性的模板继承(服务定位器模式)

objective-c - iOS架构和组件