gpt4 book ai didi

C++模板重构/泛化

转载 作者:行者123 更新时间:2023-11-28 02:42:25 26 4
gpt4 key购买 nike

能否对这段代码进行泛化,以便我可以拥有可变数量的类型/参数?

(我真的更喜欢将函数保留在结构之外,以保持代码更干净。)

unsigned int const maxID = 2 ^ 8;
typedef unsigned int ID;

template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
struct SOA {
T1 a1[maxID];
T2 a2[maxID];
T3 a3[maxID];
T4 a4[maxID];
T5 a5[maxID];
T6 a6[maxID];
ID currentMaxID = 0;
};

template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
ID AddEntity(SOA<T1, T2, T3, T4, T5, T6>& soa, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6) {
soa.a1[soa.currentMaxID] = a1;
soa.a2[soa.currentMaxID] = a2;
soa.a3[soa.currentMaxID] = a3;
soa.a4[soa.currentMaxID] = a4;
soa.a5[soa.currentMaxID] = a5;
soa.a6[soa.currentMaxID] = a6;
return soa.currentMaxID++;
}

template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
ID RemoveEntity(SOA<T1, T2, T3, T4, T5, T6>& soa, ID entityID) {
soa.currentMaxID--;
soa.a1[entityID] = soa.a1[soa.currentMaxID];
soa.a2[entityID] = soa.a2[soa.currentMaxID];
soa.a3[entityID] = soa.a3[soa.currentMaxID];
soa.a4[entityID] = soa.a4[soa.currentMaxID];
soa.a5[entityID] = soa.a5[soa.currentMaxID];
soa.a6[entityID] = soa.a6[soa.currentMaxID];
}

最佳答案

是的。

using ID = unsigned int;;
ID const maxID = 1<<8;

template <typename... Ts>
struct SOA {
std::tuple< std::array< Ts, maxID >... > a;
ID currentMaxID = 0;
};

C++14 中不需要的一些样板文件:(用 std::decay_tstd::integral_sequence<unsigned 替换它们)。

template<unsigned...>struct indexes{using type=indexes;};
template<unsigned Max, unsigned...Is>struct make_indexes:make_indexes<Max-1,Max-1,Is...>{};
template<unsigned...Is>struct make_indexes<0,Is...>:indexes<Is...>{};
template<unsigned Max>using make_indexes_t=typename make_indexes<Max>::type;
template<class T>using decay_t=typename std::decay<T>::type;

实际工作。一、 helper details :

namespace details {
template <typename... Ts, unsigned... Is>
ID AddEntity(indexes<Is...>, SOA<Ts...>& soa, decay_t<Ts> const&... ts) {
int unused[] = { ( (std::get<Is>(soa.a)[soa.currentMaxID] = ts ), void(), 0 )..., 0 };
(void)(unused); // just blocks warnings
return soa.currentMaxID++;
}
template <typename... Ts, unsigned... Is>
void RemoveEntity(indexes<Is...>, SOA<Ts...>& soa, ID entityID) {
--soa.currentMaxID;
int unused[] = {
( (std::get<Is>(soa.a)[entityId] = std::get<Is>(soa.a)[soa.currentMaxID] ),
void(), 0 )..., 0
};
(void)(unused); // just blocks warnings
}
}

它使用数组来做很多事情的技巧。然后:

template <typename... Ts>
ID AddEntity(SOA<Ts...>& soa, decay_t<Ts> const&... ts) {
return details::AddEntity( make_indexes_t< sizeof...(Ts) >{}, soa, ts... );
}

template <typename...Ts>
void RemoveEntity(SOA<Ts...>& soa, ID entityID) {
details::RemoveEntity( make_indexes_t< sizeof...(Ts) >{}, soa, entityID );
}

or something like that (live example) .

注意我支持Ts...的空集合.

关于C++模板重构/泛化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25494319/

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