助推聚变类型偏移
Boost Fusion Types offsetof
本文关键字:类型 更新时间:2023-10-16
我目前正试图计算一个数据成员的偏移量在一个增强融合适应结构,但我不确定是否有一个优雅的方式来做到这一点。我想做如下的事情:
#include <iostream>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/sequence/intrinsic/at.hpp>
#include <boost/fusion/include/at.hpp>
#include <cstddef.h>
struct test {
int a;
char c;
double b;
};
BOOST_FUSION_ADAPT_STRUCT(
test,
(int, a)
(char, c)
(double, b)
)
int main() {
test s{1, 2, 3.0};
// The following code doesn't work... I'm just trying to get my point across
std::cout << "offset is :" << offsetof(test, at<1>(s)) << std::endl;
}
关键是我不想在offsetof函数中明确地声明'a'或'b'或'c'。这本身并不是那么有用,但我想在boost for_each循环中使用它,这样我就可以在编译时计算任何结构体的所有数据成员的偏移量。
如果你有什么想法,我很乐意听听!
我不是专家,但我认为由于offsetof
的性质,如果不需要使用宏,就没有办法实现您想要的。下面的示例简单地使用一个宏为结构体的每个成员创建一个trait,当您访问它时,该trait返回其预先计算的偏移量。
在Wandbox上运行
#include <iostream>
#include <string>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/at.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <boost/fusion/include/mpl.hpp>
#include <boost/mpl/range_c.hpp>
#include <cstddef>
namespace not_fusion
{
template <typename StructName, int N>
struct offset_of;
}
//Actually creates each respective 'offset_of' trait
//you could change 'BOOST_STATIC_CONSTANT(std::size_t,...)'
//with a 'static constexpr std::size_t ...' or whatever you like
#define CREATE_OFFSET_TRAIT(R,STRUCT_NAME,INDEX,MEMBER)
template <> struct offset_of<STRUCT_NAME, INDEX>{ BOOST_STATIC_CONSTANT(std::size_t, value = offsetof(STRUCT_NAME,MEMBER)); };
//Iterates the struct members in order to create the corresponding 'offset_of' traits
#define NOT_FUSION_SAVE_OFFSETS(STRUCT_NAME,MEMBERS)
namespace not_fusion {
BOOST_PP_SEQ_FOR_EACH_I(CREATE_OFFSET_TRAIT,STRUCT_NAME,MEMBERS)
}
//Simply "invokes" 'BOOST_FUSION_ADAPT_STRUCT' and 'NOT_FUSION_SAVE_OFFSETS'
#define ADAPT_STRUCT_AND_SAVE_OFFSETS(TYPE,...)
BOOST_FUSION_ADAPT_STRUCT(TYPE,__VA_ARGS__)
NOT_FUSION_SAVE_OFFSETS(TYPE,BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
struct test1 {
int a;
char c;
double b;
};
struct test2 {
char c;
short s;
int i;
};
ADAPT_STRUCT_AND_SAVE_OFFSETS(test1,a,c,b);
ADAPT_STRUCT_AND_SAVE_OFFSETS(test2,c,s,i);
template <typename Struct>
void print_offsets(const std::string& name,const Struct&)
{
//This could be changed to use 'std::integer_sequence' instead of mpl
std::cout << "Offsets for " << name << ":" << std::endl;
typedef boost::mpl::range_c<unsigned, 0, boost::fusion::result_of::size<Struct>::value > Indices;
boost::fusion::for_each(Indices(),
[](auto index)
{
std::cout << boost::fusion::extension::struct_member_name<Struct,index>::call() << " -> " << not_fusion::offset_of<Struct,index>::value << std::endl;
}
);
}
int main() {
test1 t1{1, 2, 3.0};
test2 t2{1,2,3};
print_offsets("test1",t1);
print_offsets("test2",t2);
}
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 构造函数正在调用一个使用当前类类型的函数
- 我应该使用什么来代替void作为变体中的替代类型之一
- 类中的字符串不命名类型