自动生成代码以在c ++中打印struct的每个字段

时间:2018-03-16 06:48:04

标签: c++ eclipse editor auto-generate

我有如下结构,同样可能有多个结构有多个字段。

struct A
    {
        int a;
        int b;
        char * c;
        float d
    };

现在,如果我想打印上述结构的每个字段,我需要手动输入,

cout<<A.a<<endl;
cout<<A.b<<endl;
cout<<A.c<<endl;
cout<<A.d<<endl;

正如你所看到的,上面的东西是手动和重复的任务,有什么方法可以自动生成上面的东西。 如果任何人都可以为eclipse提供代码模板那么这将是有用的。

3 个答案:

答案 0 :(得分:0)

我认为您可以在struct A中创建一个打印所有成员的方法。无论您需要使用A.PrintInternalData()打印所有数据,都可以将其调用。

#include<iostream>

struct A
{
    int a;
    int b;
    char * c;
    float d

    void PrintInternalData()
    {
        std::cout<<A.a<<std::endl;
        std::cout<<A.b<<std::endl;
        std::cout<<A.c<<std::endl;
        std::cout<<A.d<<std::endl;
    }
};

答案 1 :(得分:0)

C ++本身不支持反射或内省。您无法检查类型并查询它包含的成员或函数。您几乎无法“手动”或使用添加反射的库(如boost反射或Qt,它也为QObject派生类提供有限版本)。

答案 2 :(得分:0)

存在一种自动枚举和输出任何结构/类的所有字段的方法,但它可能仅从 C++20 标准开始通过解包操作(例如 auto [a, b] = obj;)存在。下一个代码解决您的任何结构的任务,您的结构用作示例,有关用法示例,请参见代码末尾的 main() 函数:

Try it online!

#include <iostream>
#include <tuple>
#include <type_traits>

template <auto I>
struct any_type {
    template <class T> constexpr operator T &() const noexcept;
    template <class T> constexpr operator T &&() const noexcept;
};

template <class T, auto... Is>
constexpr auto detect_fields_count(std::index_sequence<Is...>) noexcept {
    if constexpr (requires { T{any_type<Is>{}...}; })
        return sizeof...(Is);
    else
        return detect_fields_count<T>(std::make_index_sequence<sizeof...(Is) - 1>{});
}

template <class T>
constexpr auto fields_count() noexcept {
    return detect_fields_count<T>(std::make_index_sequence<sizeof(T)>{});
}

template <class S>
constexpr auto to_tuple(S & s) noexcept {
    constexpr auto count = fields_count<S>();
    if constexpr (count == 8) {
        auto & [f0, f1, f2, f3, f4, f5, f6, f7] = s;
        return std::tie(f0, f1, f2, f3, f4, f5, f6, f7);
    } else if constexpr (count == 7) {
        auto & [f0, f1, f2, f3, f4, f5, f6] = s;
        return std::tie(f0, f1, f2, f3, f4, f5, f6);
    } else if constexpr (count == 6) {
        auto & [f0, f1, f2, f3, f4, f5] = s;
        return std::tie(f0, f1, f2, f3, f4, f5);
    } else if constexpr (count == 5) {
        auto & [f0, f1, f2, f3, f4] = s;
        return std::tie(f0, f1, f2, f3, f4);
    } else if constexpr (count == 4) {
        auto & [f0, f1, f2, f3] = s;
        return std::tie(f0, f1, f2, f3);
    } else if constexpr (count == 3) {
        auto & [f0, f1, f2] = s;
        return std::tie(f0, f1, f2);
    } else if constexpr (count == 2) {
        auto & [f0, f1] = s;
        return std::tie(f0, f1);
    } else if constexpr (count == 1) {
        auto & [f0] = s;
        return std::tie(f0);
    } else if constexpr (count == 0) {
        return std::tie();
    }
}

struct A {
    int a;
    int b;
    char const * c;
    float d;
};

int main() {
    A a{.a = 1, .b = 2, .c = "c", .d = 3.14};
    std::apply([](auto const &... x) {
        ((std::cout << x << std::endl), ...); }, to_tuple(a));
}

输出:

1
2
c
3.14