C++ Logo

std-proposals

Advanced search

[std-proposals] operator typename+ (Tuple -> ParamPack)

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sun, 12 Nov 2023 17:15:35 +0000
Let's say we have an opaque type in our program, and for the time being
it's defined as follows, but it might change in the future:

    typedef tuple<int,char*,float> Tup;

This type is passed as an argument to a non-template function:

    void Func(Tup&);

Inside 'Func', we need to create an object of the following type:

    template<typename... Ts>
    struct NeedsParamPack {
        variant<Ts...> v;
        tuple <Ts...> t;
     };

The parameter pack used to instantiate NeedsParamPack should be the same as
the parameter pack in our 'Tup' type.

We can do this as follows:

#include <iostream>
#include <tuple>
#include <variant>
using namespace std;

template<typename... Ts>
struct NeedsParamPack {
    variant<Ts...> v;
    tuple <Ts...> t;
};

template< template<typename...> class, typename > struct Helper;
template< template<typename...> class A, typename... Ts,
template<typename...> class B> struct Helper<A, B<Ts...> > {
    using T = A<Ts...>;
};

typedef tuple<int,char*,float> Tup;

void Func(Tup &tup)
{
    Helper<NeedsParamPack,Tup>::T npp;
}

I want to focus on just one line here:

      Helper<NeedsParamPack,Tup>::T npp;

What if the C++ language were to be augmented so that this line could be
simplified to the following:

      NeedsParamPack< Tup+ >::T npp;

The operator, typename+ , turns a tuple into a parameter pack. Or it can
turn a variant into a parameter pack. Or any template class into a
parameter pack.

I propose to give this operator a little more power than this though. Let's
say we have a template class with constants for parameters:

    template<int i, int j, int k, typename A>
    class SomeClass {};

I propose that the following expression:

    SomeClass+

will yield the constants as well as the types, making the following
possible:

template<int i, int j, int k, typename A> class SomeClass1 {};

template<int i, int j, int k, typename A> class SomeClass2 {};

typedef SomeClass1<1,2,3,long> Donkey;
typedef SomeClass2<4,5,6,long> Monkey;

Donkey Func(Monkey const &arg)
{
    Donkey< Monkey+ > obj(arg);
    return obj;
}

Here's a few other possible syntaxes:

    Donkey< __params(Monkey) > obj;
    Donkey< _Params(Monkey) > obj;
    Donkey< Monkey->... > obj;
    Donkey< ...Monkey... > obj;
    Donkey< [Monkey...] > obj;
    Donkey< @Monkey@ > obj;

Received on 2023-11-12 17:15:38