Skip to content

Commit

Permalink
Update akchilov2.cpp
Browse files Browse the repository at this point in the history
修复在GCC和clang下模板匹配的问题,但是在GCC下运行结果错误,应该是GCC的结构体用了和不一样的存储结构
  • Loading branch information
AzrBrk authored Dec 30, 2023
1 parent c23e80c commit 751cf02
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/群友提交/第10题/akchilov2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,21 @@ namespace my_utilities
{
using type = TL<T, Args...>;
};

template<size_t N, class T, class TL> struct gen_list
{};

template<class T, template<class...> class TL, class ...Args> struct gen_list<0, T, TL<Args...>>
{
using type = TL;
using type = TL<Args...>;
};

template<size_t N, template<class...> class TL, class T, class ...Args> struct gen_list<N, T, TL<Args...>>
{
using type = typename gen_list<N - 1, T, TL<Args..., T>>::type;
};

template<class T, class TL> struct gen_list<0, T, TL> { using type = TL; };


template<size_t N, class pre, class bck, class T> struct replace_at
{
Expand Down Expand Up @@ -104,7 +107,6 @@ namespace my_utilities
using namespace my_utilities;



template<class T> constexpr size_t size()
{
return count_size<false, std::decay_t<T>, tl<inits, inits, inits, inits, inits, inits, inits, inits, inits, inits>, 10>::value;
Expand Down Expand Up @@ -142,18 +144,18 @@ template<size_t N, class T, template<class...> class mem_type_list, class first,
//工具类,生成成员列表
template<size_t N, class T, class TL, class saved> struct make_construct_list
{};

template<class T, template<class...> class TL, template<class...> class saved, class ...Args, class ...savedArgs> struct make_construct_list<0, T, TL<Args...>, saved<savedArgs...>>
{
using current_type = typename constructible_at_try<false, 0, T, TL<Args...>>::type;
using type = typename add_front<current_type, saved<savedArgs...>>::type;
};
template<size_t N, class T, template<class...> class saved, class TL, class ...Args> struct make_construct_list<N, T, TL, saved<Args...>>
{
using current_type = typename constructible_at_try<false, N, T, TL>::type;
using type = typename make_construct_list<N - 1, T, TL, saved<current_type, Args...>>::type;
};

template<class T, class TL, class saved> struct make_construct_list<0, T, TL, saved>
{
using current_type = typename constructible_at_try<false, 0, T, TL>::type;
using type = typename add_front<current_type, saved>::type;
};

//计算偏移
template<size_t pack_size, template<class...> class TL, class ...Typs> constexpr auto make_offset_list(TL<Typs...>)
{
Expand All @@ -163,7 +165,7 @@ template<size_t pack_size, template<class...> class TL, class ...Typs> constexpr

template<size_t pack_size, class T, class possibilities> class offset_pointer
{
using construct_list = make_construct_list<size<T>() - 1, T, possibilities, tl<>>::type;
using construct_list = typename make_construct_list<size<T>() - 1, T, possibilities, tl<>>::type;
public:
offset_pointer(T a) noexcept : baseptr(reinterpret_cast<unsigned char*>(&a)) {}
template<size_t I> typename select_element<I, construct_list>::type get() const
Expand All @@ -184,7 +186,7 @@ template<size_t pack_size, class T, class possibilities> class offset_pointer
unsigned char* baseptr;
std::array<size_t, size<T>()>
member_offset{
make_offset_list<pack_size>(make_construct_list<size<T>() - 1, T, possibilities, tl<>>::type())
make_offset_list<pack_size>(typename make_construct_list<size<T>() - 1, T, possibilities, tl<>>::type())
};
};

Expand All @@ -194,7 +196,7 @@ template<class T, class F, size_t ...I> void for_each_member_impl(T&& t, F&& f,
//有点上帝视角了,因为已经看到了所有的数据类型
using possible_types = tl<int, std::string, double>;
offset_pointer<alignof(T), T, possible_types> op{ t };
(f(op.get<I>()), ...);
(f(op.template get<I>()), ...);
}

template<class T, class F> constexpr void for_each_member(T&& t, F&& f)
Expand Down

0 comments on commit 751cf02

Please sign in to comment.