Jarod42の検索-TA BLEソリューションは、おそらく最速かつ最も簡単ですが、ちょうど完全のために、元のコードの多かれ少なかれ逐語的に交換が余分index_sequenceと
template< std::size_t I, std::size_t... Is >
void call_foo(bool switch1, bool switch2, bool switch3, std::index_sequence<I,Is...>)
{
if(switch1 == bool(I&1) && switch2 == bool(I&2) && switch3 == bool(I&4))
foo<bool(I&1),bool(I&2),bool(I&4)>();
if constexpr(sizeof...(Is) > 0)
call_foo(switch1, switch2, switch3, std::index_sequence<Is...>{});
}
// to be used as
call_foo(true, false, true, std::make_index_sequence<2*2*2>{});
かもしれないが、これはまた、任意のboolsに数える一般化することができます。
template< class F, class... T, std::size_t... Js, std::size_t I, std::size_t... Is >
void untemplatize_impl(F&& f, std::index_sequence<Js...> bits, std::index_sequence<I,Is...>, T... bools)
{
if(I == ((unsigned(bools)<<Js) | ...))
std::forward<F>(f).template operator()<bool(I&(1<<Js))...>();
if constexpr(sizeof...(Is) > 0)
untemplatize_impl(std::forward<F>(f), bits, std::index_sequence<Is...>{}, bools...);
}
template< class F, class... T > // SFINAEd, if needed
void untemplatize(F&& f, T... bools)
{
untemplatize_impl(std::forward<F>(f), std::make_index_sequence<sizeof...(T)>{}, std::make_index_sequence<(1u<<sizeof...(T))>{}, bools...);
}
// to be used as
untemplatize(foo{}, false, true, true);
だけでなく、C++ 11で可能であることと同じことを、しかし、はるかに厄介:ファンクタを取る
完全に汎用的なC++ 17のバージョンは、のように見えることがあります。
表現SFINAEおそらく[インラインリンク](http://en.cppreference.com/w/cpp/language/sfinae) –
"テンプレート化されたコードで大幅な改善"私はスピードの違いを引き起こす別の何かがあると思う。あなたが実行しているコードではテンプレートはありません... – user463035818
プロファイリング時にOPTIMIZEDビルド( '-O2'コンパイラオプションなど)を使用していて、非テンプレート関数を' inline'として定義していますか?そのコード)? –