Skip to main content
Dispel the mystery: add a comment that this uses C++17 a comma-operator fold expression.
Source Link

With C++17, the cleanest way is

template<typename T> struct tag { using type = T; }; template<typename... Ts> struct select_last { // Use a fold-expression to fold the comma operator over the parameter pack. using type = typename decltype((tag<Ts>{}, ...))::type; }; 

with O(1) instantiation depth.

With C++17, the cleanest way is

template<typename T> struct tag { using type = T; }; template<typename... Ts> struct select_last { using type = typename decltype((tag<Ts>{}, ...))::type; }; 

with O(1) instantiation depth.

With C++17, the cleanest way is

template<typename T> struct tag { using type = T; }; template<typename... Ts> struct select_last { // Use a fold-expression to fold the comma operator over the parameter pack. using type = typename decltype((tag<Ts>{}, ...))::type; }; 

with O(1) instantiation depth.

Fixed buggy implementation. Consider `select_last<void()>::type* fn;`.
Source Link
Passer By
  • 21.6k
  • 7
  • 58
  • 112

With C++17, the cleanest way is

template<typename T> Tstruct val()tag { using type = T; }; template<typename... Args>Ts> struct select_last { using type = typename decltype((val<Args>()tag<Ts>{}, ...));::type; }; 

with O(1) instantiation depth.

With C++17, the cleanest way is

template<typename T> T val(); template<typename... Args> struct select_last { using type = decltype((val<Args>(), ...)); }; 

with O(1) instantiation depth.

With C++17, the cleanest way is

template<typename T> struct tag { using type = T; }; template<typename... Ts> struct select_last { using type = typename decltype((tag<Ts>{}, ...))::type; }; 

with O(1) instantiation depth.

Source Link
Passer By
  • 21.6k
  • 7
  • 58
  • 112

With C++17, the cleanest way is

template<typename T> T val(); template<typename... Args> struct select_last { using type = decltype((val<Args>(), ...)); }; 

with O(1) instantiation depth.