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: Sat, 4 May 2024 20:03:35 +0100
On Saturday, May 4, 2024, Thiago Macieira wrote:

> On Saturday 4 May 2024 06:44:13 GMT-7 Frederick Virchanza Gotham via Std-
> Proposals wrote:
> > I wonder is it foreseeable in the future that the following would be
> valid
> > code for a program that ends with EXIT_SUCCESS?
>
> I hope so. There are many programming techniques that get unblocked by
> having
> the ability to turn formal parameters into constant expressions, when run
> in a
> constant expression environment.
>



Just to be a little more verbose here. If we consider that there are two
ways in C++ of writing a function that adds two numbers:

int Add1(int const a, int const b)
{
    return a+b;
}

template<int a, int b>
int Add2(void)
{
    return a+b;
}

Then what we want is for the compiler to encounter the following function:

constexpr int Add3(int const a, int const b)
{
    return a+b;
}

And for the compiler to internally generate both Add1 and Add2, invoking
Add2 wherever possible, and invoking Add1 only when it can't invoke Add2.

That is to say, a call to:

    Add3( monkey, frog );

will be transformed into either:

    Add2<monkey, frog>();

or:

    Add1( monkey, frog );

Received on 2024-05-04 19:03:38