I wanted to debate with you in this topic actually because I do let the pointer…
- User
I wanted to debate with you in this topic actually because I do let the pointer accessing a data block with index, so here's my work:
#include<iostream> #define ENABLE_DYNAMIC_LOCATION #include"align_memory_field.hpp" using namespace meta_typelist; struct X { int a; double b; char c; }; int main() { X x{}; align_memory_field<alignof(X), do_nothing_t/*a deallocator, here it does nothing, ignore it*/, int, double, char> field{ &x }; for (auto i = 0; i < 3; ++i) { std::cin >> field[i]; } }so this is the logic I use to calculate the offset:
template<std::size_t pack_size, std::size_t layer_offset, std::size_t offset> struct align_offset_ptr { static constexpr std::size_t layer = layer_offset;//alignment layer as base static constexpr std::size_t value = offset;//offset from the current layer template<std::size_t lay> struct advance_layer { using type = align_offset_ptr<pack_size, layer + lay, 0>; }; template<bool can_be_placed, std::size_t seek_offset> struct seek_impl { using type = align_offset_ptr<pack_size, layer, value>; }; template<std::size_t seek_offset> struct seek_impl<false, seek_offset> { using type = typename std::conditional_t< (value + seek_offset > pack_size), typename advance_layer<1>::type, align_offset_ptr<pack_size, layer_offset, value + 1> >::template seek<seek_offset>; }; template<std::size_t seek_offset> using seek = typename seek_impl<value% seek_offset == 0, seek_offset>::type; template<std::size_t advance_offset> struct advance_impl { using type = std::conditional_t<(value + advance_offset < pack_size), align_offset_ptr<pack_size, layer, value + advance_offset>, std::conditional_t< static_cast<bool>(advance_offset% pack_size), typename advance_layer<advance_offset / pack_size + 1>::type, typename advance_layer<advance_offset / pack_size>::type> >; }; template<std::size_t advance_offset> using advance = typename advance_impl<advance_offset>::type; static constexpr std::size_t offset_v = layer * pack_size + value; }; //align offset iterator impl template<class align_offset_pointer, class T> struct align_offset_advance_basic { using type = typename align_offset_pointer ::template seek<sizeof(T)> ::template advance<sizeof(T)>; }; template<class align_offset_pointer, class T> using align_offset_advance_basic_t = typename align_offset_advance_basic<align_offset_pointer, T>::type; //array in structure is considered as N members template<class align_offset_pointer, class array_t> struct align_offset_advance_array_impl {}; template<class align_offset_pointer, class basic_type, std::size_t N> struct align_offset_advance_array_impl<align_offset_pointer, basic_type[N]> { using type = typename align_offset_advance_array_impl< align_offset_advance_basic_t<align_offset_pointer, basic_type>, basic_type[N - 1]>::type; }; template<class align_offset_pointer, class basic_type> struct align_offset_advance_array_impl<align_offset_pointer, basic_type[1]> { using type = align_offset_advance_basic_t<align_offset_pointer, basic_type>; }; template<class align_offset_pointer, class array_t> using align_offset_advance_array_t = typename align_offset_advance_array_impl< align_offset_pointer, array_t >::type; template<class align_offset_pointer, class T> using align_offset_advance_t = std::conditional_t< std::is_array_v<T>, invoke_type_if<std::is_array_v<T>, align_offset_advance_array_impl<align_offset_pointer, T>>, invoke_type_if<!std::is_array_v<T>, align_offset_advance_basic<align_offset_pointer, T>> >; template<class align_offset_pointer, class T> struct align_offset_seek_impl { using type = std::conditional_t<std::is_array_v<T>, typename align_offset_pointer::template seek<sizeof(std::remove_extent_t<T>)>, typename align_offset_pointer::template seek<sizeof(T)> >; }; template<class align_offset_pointer, class T> using align_offset_seek_t = typename align_offset_seek_impl<align_offset_pointer, T>::type; template<class align_offset_pointer, class offset_map, std::size_t N> struct align_offset_iterator { using type = align_offset_pointer; }; template<class align_offset_pointer, std::size_t N, template<class ...> class offset_map, class first, class ...rest> struct align_offset_iterator<align_offset_pointer, offset_map<first, rest...>, N> { using recursive_type = align_offset_iterator< align_offset_advance_t<align_offset_pointer, first>, offset_map<rest...>, N - 1>; using type = typename recursive_type::type; using get = typename recursive_type::get; using value_type = typename recursive_type::value_type; }; template<class align_offset_pointer, template<class...> class offset_map, class first, class ...rest> struct align_offset_iterator<align_offset_pointer, offset_map<first, rest...>, 0> { //kept pointer using type = align_offset_pointer; //common interface using get = align_offset_seek_t<type, first>; //common interface for Ty info using value_type = first; }; template<class ...> struct offset_pattern {}; template<std::size_t pack_size, class ...typs> struct offset_vector { template<std::size_t I> using iterator = align_offset_iterator< align_offset_ptr<pack_size, 0, 0>, offset_pattern<typs...>, I>; template<std::size_t I> static constexpr std::size_t offset_v = iterator<I>::get::offset_v; template<std::size_t I> using value_type = typename iterator<I>::value_type; //get specified indices by any other struct like std::index_sequence or meta_traits::meta_array template<template<std::size_t ...> class indices_container, std::size_t...I> static constexpr auto offset_array(indices_container<I...>) { return indices_container<offset_v<I>...>{}; } }; - Assistant
- User
you are wrong about "In your code, you're enabling pointer-like behavior through custom utilities, such as align_offset_ptr, to compute layout offsets dynamically." you should realize that all my code are TMP, all calculation is done in compile-time, there never dynamic, just like how array is
- Assistant
- User
My work refuse accessing a no trivial structure, it can be easily done by adding facilities check like requires std::is_trivial_v, if you've noticed it is 'field{&x}', but not 'field{(void*)&x}', when you lookup all compiler's abi reference you can find out how the mainstream C++ compilers align their data, I only write this to argue with someone who insist that "in c++/c, an array is a pointer", by this implementation I only want to tell him if he insist that, there's more types could be considered pointer, not just the array... I do not wish to prove a trivial struct is an array nor a pointer, that is why I practiced with you
- Assistant
- User
also you asked "What does field[0] mean? The address of a or its value?" here I tell you what my field[1] means, it returns a iterator that is capable of type_check function when this iterator is iteracting with any operation that is trying to access the field memory, while all the type_check information is encapsuled in a meta template that cost 0 runtime in type_checking auto operator[](unsigned int idx)const { if (idx > Max_types_index) throw std::out_of_range("error: index overflow!");
using offset_array_set = typename meta_count<0, member_counts>::array; using meta_array_set = decltype(Member_offset_vector::offset_array(offset_array_set{})); using field_list = exp_if<Is_self_destructible, exp_list<Args...>, exp_list<Self_destruct_determin_t, Args...>>; using type_check_stream = meta_stream_o<member_counts, meta_replace_o, meta_istream<field_list>>; using type_check_looper = align_memory_helper::align_memory_iterator<while_constexpr<type_check_stream>>; return type_check_looper{base_ptr, idx, meta_array_set::array()[idx]};}
- Assistant