Skip to content

Instantly share code, notes, and snippets.

@buffyanamin
Created February 23, 2022 06:16
Show Gist options
  • Save buffyanamin/406002d5dda51040531ff379ca33e08b to your computer and use it in GitHub Desktop.
Save buffyanamin/406002d5dda51040531ff379ca33e08b to your computer and use it in GitHub Desktop.
c++ template specify type after variadic types
// from https://www.cppstories.com/2020/09/variadic-pack-first.html/
namespace detail
{
template<typename Output, typename... Inputs>
void tempFunc(Output& output, Inputs const&... inputs)
{
// implementation of f
}
template<typename... InputsThenOutput, size_t... InputIndexes>
void tempFunc(std::tuple<InputsThenOutput...> inputsThenOutputs, std::index_sequence<InputIndexes...>)
{
auto constexpr OutputIndex = sizeof...(InputsThenOutput) - 1;
detail::tempFunc(std::get<OutputIndex>(inputsThenOutputs), std::get<InputIndexes>(inputsThenOutputs)...);
}
}
// usage: tempFunc(input1, input2, input3, output);
template<typename... InputsThenOutput>
void tempFunc(InputsThenOutput&&... inputsThenOutput)
{
detail::tempFunc(std::forward_as_tuple(inputsThenOutput...), std::make_index_sequence<sizeof...(inputsThenOutput) - 1>{});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment