Created
March 4, 2020 08:56
-
-
Save oliora/5daffbede00577db8799f37c929a7918 to your computer and use it in GitHub Desktop.
type_t implementation and example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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