C++ Logo

std-proposals

Advanced search

[std-proposals] Specify that argument to function cannot be an rvalue

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sat, 8 Oct 2022 22:46:44 +0100
This year I wrote a function something like as follows:

    long unsigned Func(long unsigned &arg)
    {
        return arg + 5u;
    }

You can see that 'arg' isn't altered by this function, and so the
function parameter type could be made a reference to const. I left it
as non-const though, because I didn't want someone to be able to do
the following:

    short unsigned k = 7u;

    Func(k);

I didn't want the implicit conversion from rvalue to "reference to const".

I think there should be a way of telling the compiler to disallow this
implicit conversion when passing arguments to a function, for example
using the caret symbol as follows:

    long unsigned Func(long unsigned const &^arg)
    {
        return arg + 5u;
    }

With regard to parsing and globbing, the two symbols & and ^ side by
side could be globbed together as one token, and could be also be used
as follows:

    int main(void)
    {
        int n= 4;

        int const &^x = n; // This is fine, it compiles

        int const &^y = 7; // This fails to compile (no implicit
rvalue conversion)

        int const &z = n;

        int const &^w = z; // This is allowed (because z is an lvalue)

        int const &b = 73;

        int const &^c = b; // This is allowed (even though it
started out as an rvalue)
    }

Received on 2022-10-08 21:46:54