gpt4 book ai didi

c++ - 确定模板中结构或元组的成员偏移量

转载 作者:太空狗 更新时间:2023-10-29 21:32:29 25 4
gpt4 key购买 nike

我想编写一个将表格写入 HDF5 文件的模板函数。签名应该类似于

template<typename record> void writeTable(const std::vector<record>& data);

其中 record 是一个结构,或者

template<typename... elements> 
void writeTable(const std::vector<std::tuple<elements...>>& data);

实际的实现会有更多的参数来确定目的地等。

要写入数据,我需要定义一个 HDF5 复合类型,其中包含成员的名称和偏移量。通常你会使用 HOFFSET宏获取字段偏移量,但由于我事先不知道结构字段,所以我不能这样做。

到目前为止,我尝试的是从类型名称包构建结构类型。天真的实现没有标准布局,但实现 here做。剩下的就是获取成员的偏移量。我想将参数包扩展到具有偏移量的初始化列表中:

#include <vector>

template<typename... members> struct record {};

template<typename member, typename... members> struct record<member, members...> :
record<members...> {
record(member m, members... ms) : record<members...>(ms...), tail(m) {}
member tail;
};

template<typename... Args> void
make_table(const std::string& name, const std::vector<record<Args...>>& data) {
using record_type = record<Args...>;
std::vector<size_t> offsets = { get_offset(record_type,Args)... };
}

int main() {
std::vector<record<int, float>> table = { {1, 1.0}, {2, 2.0} };
make_table("table", table);
}

get_offset 是否可以实现? ?我认为不会,因为在 record<int, int> 的情况下这将是模棱两可的。还有其他方法吗?

或者还有其他方法可以解决这个问题吗?

最佳答案

计算偏移量非常简单。给定一个类型为 T0、T1 ... TN 的元组。 T0 的偏移量是0 (只要您在 alignas(T0) 数组上使用 charT1 的偏移量是 sizeof(T0) 四舍五入到 alignof(T1)

一般来说,TB的偏移量(在 TA 之后)是 round_up(offset_of<TA>() + sizeof(TA), alignof(TB)) .

计算 std::tuple 中元素的偏移量可以这样做:

constexpr size_t roundup(size_t num, size_t multiple) {
const size_t mod = num % multiple;
return mod == 0 ? num : num + multiple - mod;
}

template <size_t I, typename Tuple>
struct offset_of {
static constexpr size_t value = roundup(
offset_of<I - 1, Tuple>::value + sizeof(std::tuple_element_t<I - 1, Tuple>),
alignof(std::tuple_element_t<I, Tuple>)
);
};

template <typename Tuple>
struct offset_of<0, Tuple> {
static constexpr size_t value = 0;
};

template <size_t I, typename Tuple>
constexpr size_t offset_of_v = offset_of<I, Tuple>::value;

这是一个测试套件。从第一个测试中可以看出,考虑了元素的对齐方式。

static_assert(offset_of_v<1, std::tuple<char, long double>> == 16);
static_assert(offset_of_v<2, std::tuple<char, char, long double>> == 16);
static_assert(offset_of_v<3, std::tuple<char, char, char, long double>> == 16);
static_assert(offset_of_v<4, std::tuple<char, char, char, char, long double>> == 16);

static_assert(offset_of_v<0, std::tuple<int, double, int, char, short, long double>> == 0);
static_assert(offset_of_v<1, std::tuple<int, double, int, char, short, long double>> == 8);
static_assert(offset_of_v<2, std::tuple<int, double, int, char, short, long double>> == 16);
static_assert(offset_of_v<3, std::tuple<int, double, int, char, short, long double>> == 20);
static_assert(offset_of_v<4, std::tuple<int, double, int, char, short, long double>> == 22);
static_assert(offset_of_v<5, std::tuple<int, double, int, char, short, long double>> == 32);

我在上述测试中对偏移量进行了硬编码。如果以下测试成功,则偏移量是正确的。

static_assert(sizeof(char) == 1 && alignof(char) == 1);
static_assert(sizeof(short) == 2 && alignof(short) == 2);
static_assert(sizeof(int) == 4 && alignof(int) == 4);
static_assert(sizeof(double) == 8 && alignof(double) == 8);
static_assert(sizeof(long double) == 16 && alignof(long double) == 16);

std::tuple似乎按顺序存储它的元素(不对它们进行排序以优化填充)。以下测试证明了这一点。我认为标准不需要 std::tuple以这种方式实现,所以我认为以下测试不能保证成功。

template <size_t I, typename Tuple>
size_t real_offset(const Tuple &tup) {
const char *base = reinterpret_cast<const char *>(&tup);
return reinterpret_cast<const char *>(&std::get<I>(tup)) - base;
}

int main(int argc, char **argv) {
using Tuple = std::tuple<int, double, int, char, short, long double>;
Tuple tup;
assert((offset_of_v<0, Tuple> == real_offset<0>(tup)));
assert((offset_of_v<1, Tuple> == real_offset<1>(tup)));
assert((offset_of_v<2, Tuple> == real_offset<2>(tup)));
assert((offset_of_v<3, Tuple> == real_offset<3>(tup)));
assert((offset_of_v<4, Tuple> == real_offset<4>(tup)));
assert((offset_of_v<5, Tuple> == real_offset<5>(tup)));
}

既然我已经付出了所有这些努力,那么real_offset会不会功能适合您的需求?


这是访问 char[] 的元组的最小实现与 offset_of .这是未定义的行为,因为 reinterpret_cast .即使我以相同的字节构造对象并以相同的字节访问对象,它仍然是 UB。参见 this answer对于所有标准人。它适用于你能找到的每个编译器,但它是 UB,所以无论如何都要使用它。这个元组是标准布局(不像 std::tuple )。如果你的元组的元素都是可复制的,你可以删除复制和移动构造函数并将它们替换为 memcpy .

template <typename... Elems>
class tuple;

template <size_t I, typename Tuple>
struct tuple_element;

template <size_t I, typename... Elems>
struct tuple_element<I, tuple<Elems...>> {
using type = std::tuple_element_t<I, std::tuple<Elems...>>;
};

template <size_t I, typename Tuple>
using tuple_element_t = typename tuple_element<I, Tuple>::type;

template <typename Tuple>
struct tuple_size;

template <typename... Elems>
struct tuple_size<tuple<Elems...>> {
static constexpr size_t value = sizeof...(Elems);
};

template <typename Tuple>
constexpr size_t tuple_size_v = tuple_size<Tuple>::value;

constexpr size_t roundup(size_t num, size_t multiple) {
const size_t mod = num % multiple;
return mod == 0 ? num : num + multiple - mod;
}

template <size_t I, typename Tuple>
struct offset_of {
static constexpr size_t value = roundup(
offset_of<I - 1, Tuple>::value + sizeof(tuple_element_t<I - 1, Tuple>),
alignof(tuple_element_t<I, Tuple>)
);
};

template <typename Tuple>
struct offset_of<0, Tuple> {
static constexpr size_t value = 0;
};

template <size_t I, typename Tuple>
constexpr size_t offset_of_v = offset_of<I, Tuple>::value;

template <size_t I, typename Tuple>
auto &get(Tuple &tuple) noexcept {
return *reinterpret_cast<tuple_element_t<I, Tuple> *>(tuple.template addr<I>());
}

template <size_t I, typename Tuple>
const auto &get(const Tuple &tuple) noexcept {
return *reinterpret_cast<tuple_element_t<I, Tuple> *>(tuple.template addr<I>());
}

template <typename... Elems>
class tuple {
alignas(tuple_element_t<0, tuple>) char storage[offset_of_v<sizeof...(Elems), tuple<Elems..., char>>];
using idx_seq = std::make_index_sequence<sizeof...(Elems)>;

template <size_t I>
void *addr() {
return static_cast<void *>(&storage + offset_of_v<I, tuple>);
}

template <size_t I, typename Tuple>
friend auto &get(const Tuple &) noexcept;

template <size_t I, typename Tuple>
friend const auto &get(Tuple &) noexcept;

template <size_t... I>
void default_construct(std::index_sequence<I...>) {
(new (addr<I>()) Elems{}, ...);
}
template <size_t... I>
void destroy(std::index_sequence<I...>) {
(get<I>(*this).~Elems(), ...);
}
template <size_t... I>
void move_construct(tuple &&other, std::index_sequence<I...>) {
(new (addr<I>()) Elems{std::move(get<I>(other))}, ...);
}
template <size_t... I>
void copy_construct(const tuple &other, std::index_sequence<I...>) {
(new (addr<I>()) Elems{get<I>(other)}, ...);
}
template <size_t... I>
void move_assign(tuple &&other, std::index_sequence<I...>) {
(static_cast<void>(get<I>(*this) = std::move(get<I>(other))), ...);
}
template <size_t... I>
void copy_assign(const tuple &other, std::index_sequence<I...>) {
(static_cast<void>(get<I>(*this) = get<I>(other)), ...);
}

public:
tuple() noexcept((std::is_nothrow_default_constructible_v<Elems> && ...)) {
default_construct(idx_seq{});
}
~tuple() {
destroy(idx_seq{});
}
tuple(tuple &&other) noexcept((std::is_nothrow_move_constructible_v<Elems> && ...)) {
move_construct(other, idx_seq{});
}
tuple(const tuple &other) noexcept((std::is_nothrow_copy_constructible_v<Elems> && ...)) {
copy_construct(other, idx_seq{});
}
tuple &operator=(tuple &&other) noexcept((std::is_nothrow_move_assignable_v<Elems> && ...)) {
move_assign(other, idx_seq{});
return *this;
}
tuple &operator=(const tuple &other) noexcept((std::is_nothrow_copy_assignable_v<Elems> && ...)) {
copy_assign(other, idx_seq{});
return *this;
}
};

或者,您可以使用此功能:

template <size_t I, typename Tuple>
size_t member_offset() {
return reinterpret_cast<size_t>(&std::get<I>(*static_cast<Tuple *>(nullptr)));
}

template <typename Member, typename Class>
size_t member_offset(Member (Class::*ptr)) {
return reinterpret_cast<size_t>(&(static_cast<Class *>(nullptr)->*ptr));
}

template <auto MemPtr>
size_t member_offset() {
return member_offset(MemPtr);
}

再一次,这是未定义的行为(因为 nullptr 取消引用和 reinterpret_cast )但它会按预期与每个主要编译器一起工作。函数不能是 constexpr (即使成员偏移量是编译时计算)。

关于c++ - 确定模板中结构或元组的成员偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55071632/

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