這是TTL:類型列表運行時typeswitch作爲開關而不是嵌套if?
////////////////////////////////////////////////////////////
// run-time type switch
template <typename L, int N = 0, bool Stop=(N==length<L>::value) > struct type_switch;
template <typename L, int N, bool Stop>
struct type_switch
{
template< typename F >
void operator()(size_t i, F& f)
{
if(i == N)
{
f.operator()<typename impl::get<L,N>::type>();
}
else
{
type_switch<L, N+1> next;
next(i, f);
}
}
};
它用於typeswitching一個類型串。問題是 - 他們通過一系列嵌套的if來做到這一點。有沒有辦法將這種類型的開關做爲單個select語句來代替?
謝謝!
實際使用的這種結構是什麼? – Potatoswatter 2010-01-28 19:47:04
最新的clang可以將這種東西轉換成跳轉表:https://godbolt.org/g/Nco0Al,GCC不能(具有相同的選項):https://godbolt.org/g/4VL9e7。 – user877329 2017-02-11 12:33:03