gpt4 book ai didi

c++ - 自动生成代码以在 C++ 中打印结构的每个字段

转载 作者:太空宇宙 更新时间:2023-11-04 12:50:34 26 4
gpt4 key购买 nike

我的结构如下,同样可以有多个结构和多个字段。

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 的代码模板,那将很有用。

最佳答案

有一种方法可以自动枚举和输出任何结构/类的所有字段,但它可能仅从 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

关于c++ - 自动生成代码以在 C++ 中打印结构的每个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49314724/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com