C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Function only accepts parameter that will persist

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 6 May 2024 04:43:40 +0100
On Mon, May 6, 2024 at 12:20 AM Frederick Virchanza Gotham wrote:
>
> Now obviously the following paper is way too simplistic and needs to
> be beefed out considerably . . . but it's a start:
>
> http://www.virjacode.com/papers/consteval_template_instantiation.htm


And it would make it possible to do a few other things too, such as
having different return types depending upon the argument:

    consteval auto Monkey(std::initializer_list<std::string_view> const args)
    {
        if constexpr ( args.size() % 2u )
        {
            return 5; // returns int
        }
        else
        {
            return 2.5; // returns double
        }
    }

And then 'decltype' could be used to get the return type without
invoking the function:

    typedef decltype( Monkey("frog","cat","fish") ) MyType;

Received on 2024-05-06 03:43:47