Skip to content

Instantly share code, notes, and snippets.

@oliora
Created March 4, 2020 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oliora/5daffbede00577db8799f37c929a7918 to your computer and use it in GitHub Desktop.
Save oliora/5daffbede00577db8799f37c929a7918 to your computer and use it in GitHub Desktop.
type_t implementation and example
template<class T, typename... Ts>
using type_t = T;
// Alternative implementation for compilers before
// [CWG 1558](http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1558)
// template<class T, typename... Ts> struct type_t_impl { typedef T type;};
// template<class T, typename... Ts> using type_t = typename type_t_impl<T, Ts...>::type;
// Usage example:
// A bunch of overloads:
void encode(SomeType& out, const AnotherType& in);
void encode(SomeOtherType& out, const YetAnotherType& in);
// ...
// Generic wrapper which is SFINAE-friendly and does not require introducing extra traits:
template<class Out, class In>
inline auto encode_to(In&& in) -> type_t<Out, decltype(encode(std::declval<Out&>(), std::forward<In>(in)))>
{
Out out;
encode(out, std::forward<In>(in));
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment