0

For educational purposes (while reading book Modern C++ Design - A Alexandrescu) I want to write some helper to delete all types in parameter pack before type T.

For example delete_until_T<C,A,B,C,D> must be opened like tuple<C,D> or just <C,D>.

As I understand it may be done in old enough recursive way and non recursive way with C++17.

In this piece of code i wanna do it in non recursive way using std::conditional.

//https://stackoverflow.com/a/23863962/12575933 - based on this answer
#include <tuple>
#include <type_traits>

template<typename...Ts>
using tuple_cat_t = decltype(std::tuple_cat(std::declval<Ts>()...));

template<class T, class ... Ts>
using delete_untill_T = tuple_cat_t
<
    typename std::conditional
    <
    std::is_same_v<T,Ts>||bool_func<>,
    std::tuple<Ts>,
    std::tuple<>
    >::type...
> ;

The first question is how to make a variable or function or struct which value can be changed during this parameter pack opening and used in std::conditional? I want to make bool_func<> which returns false before std::is_same returns true while opening, and after that returns false.

If it's possible, my second question is how to remove tuple and pass just parameters in parameter pack to template function.

Or may be there is another way to delete types before T? (recursive algorithm will do). Or with C++ 20 features?

cigien
  • 57,834
  • 11
  • 73
  • 112
  • What are the dolphins teaching? And why do they want this task done? – Yakk - Adam Nevraumont Mar 14 '21 at 18:33
  • What does Alexandrescu teach? Who knows what all this means? Call the author dude:] If you open this book of 2002, then at paragraph 3.13.3, tells about linear hierarchies. In C ++ 98, Typelist was used, and now, as I understand parameter pack for these purposes. So when I played with the code, the access to the hierarchy objects is carried out through `static_cast` And in order to make the access of the object through a clear interface like `obj.virtual_func(type);` here I need this function. Line 400-427 [link](https://github.com/ikvasir/modern_cpp_design) Confirm? – Alexey Chicherin Mar 14 '21 at 21:33
  • I am talking about the educational porpuses, not Alexandrescu. – Yakk - Adam Nevraumont Mar 15 '21 at 04:15
  • Clarify your question. – Alexey Chicherin Mar 15 '21 at 13:55
  • You typed "educational porpuses", and I was asking about what made those [dolphins](https://en.wikipedia.org/wiki/Porpoise) educational. That is all, a bit of a pun. – Yakk - Adam Nevraumont Mar 15 '21 at 14:53
  • [link](https://www.youtube.com/watch?v=ZIk74rOgZeA) – Alexey Chicherin Mar 16 '21 at 09:52
  • [link2](https://dictionary.cambridge.org/example/english/educational-purpose) – Alexey Chicherin Mar 16 '21 at 09:53

1 Answers1

2

One way of implementing this is to use partial specializations to match when the type that you're looking for is found in the variadic parameters.

// recursive case, first 2 template parameters don't match
template<typename T, typename, typename ...Ts>
struct delete_until_impl : delete_until_impl<T, Ts...> {};  

// base case, first 2 parameters match
template<typename T, typename ...Ts>
struct delete_until_impl<T, T, Ts...> 
{
    using type = std::tuple<T, Ts...>;
};

// convenience alias to avoid having to say typename everywhere
template<typename ...Ts>
using delete_until_T = typename delete_until_impl<Ts...>::type;

Here's a demo.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • Is it possible to pass Ts... into type without tuple? like `using type = Ts...;` – Alexey Chicherin Mar 16 '21 at 13:39
  • @AlexeyChicherin No, I don't think so. `type` is a *single* type, not a list of types, so you have to wrap all those `Ts...` into some type, for example `tuple`. – cigien Mar 16 '21 at 13:41