C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Extreme Template Parameter Possibilities

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 28 Nov 2022 16:52:21 +0000
On Mon, Nov 28, 2022 at 4:27 PM Frederick Virchanza Gotham
<cauldwell.thomas_at_[hidden]> wrote:

> template<text special_cleanup>
> int CloseHandle(int const arg)
> {
> StopComms(arg);
> SetInactive(arg);
>
> {<special_cleanup>};
>
> CloseFile(arg);
> }


Of course with the above example, we could have just passed an
"std::function<void(void)>" as a normal function parameter, but if we
could copy-paste arbitrary text into a template function then we could
do the following:

    CloseHandle<"if constexpr(std::is_class_v<SomeType>) return;">();

We could return prematurely from a function, or we could change a
stack variable into a static-duration variable:

    template <text T>
    int Func(int arg)
    {
        {<T>} int count = 0;

        return ++count + arg;
    }

And then invoke it like this:

    Func<"static">(5);

Received on 2022-11-28 16:52:33