Date: Sun, 19 Oct 2025 22:24:06 +0000
Sorry, Google Mail sent it prematurely again... I think it happens
when I do Ctrl + New Line. Here's my 3rd attempt:
If you're averse to half-baked ideas that haven't been dissected yet,
then this isn't the post for you.
About a year ago I was proposing an idea to allow a function to take a
PRvalue as a parameter, and in the past month another guy was
proposing something along similar lines.
My main aim with this only-half-thought-out proposal is that existing
code doesn't need to be changed. So let's say we have a template
function in a header file something like:
template<typename T>
void Func(T &&arg)
{
T var( std::forward<T>(arg) );
}
Imagine if we had a function that returns a string by value as follows:
extern string SomeLibraryFunc( int a, double b );
Now imagine if we could use a new keyword called 'prvalue' as follows:
int main(void)
{
prvalue x = SomeLibraryFunction( 8, 34.4 );
Func(x);
}
The compiler would have its work cut out for it here. Normally when
you write a function and give it a forwarding reference for a
parameter (i.e. T&&), the compiler only has to generate two real
functions (one that takes an Lvalue, one that takes an Rvalue). On
x86_64 Linux, the address of the argument would be passed in the RDI
register. But in the above code snippet, where you see the line
"Func(x);", the compiler would have to generate an implementation of
'Func' that receives a function pointer in RDI. This function pointer
would point to a function like this:
string CompilerGenerated000(void)
{
SomeLibraryFunction( 8, 34.4 );
}
And then within the implementation of "Func", this function pointer
would have to be invoked something like:
template<typename T>
void Func(T &&arg)
{
T var( arg() );
}
So the machine code for 'Func' accepts a function pointer in the RDI
register. The function pointer points to a function that returns the
PRalue, something like:
string CompilerGenerated000(void)
{
SomeLibraryFunction( 8, 34.4 );
}
and then this function pointer gets invoked in order to put the
PRvalue inside 'var' without the need for any copying or moving.
So let's look back at the following line:
prvalue x = SomeLibraryFunction( 8, 34.4 );
In the above line, which looks live a variable definition with
initialisation, we get some sort of thing called 'x'. 'x' is not a
variable in the sense that we already understand variables. It's more
like an 'invokable thing that can yield a PRvalue', almost like a kind
of 'vehicle' or 'promissory note' toward getting a PRvalue.
If we were able to create 'prvalue' thingies like this, we could then
pass them to template functions, and then effectively the template
function would be accepting a PRvalue as an argument. The best thing
about all of this is that none of the header files need to be edited
-- this new 'prvalue' feature could be used wiith pre-existing header
files that don't need to be changed.
I'm actually finished the post now -- I'm not keen on webmail sites
that try to interpret a few different hotkeys as 'Send'.
when I do Ctrl + New Line. Here's my 3rd attempt:
If you're averse to half-baked ideas that haven't been dissected yet,
then this isn't the post for you.
About a year ago I was proposing an idea to allow a function to take a
PRvalue as a parameter, and in the past month another guy was
proposing something along similar lines.
My main aim with this only-half-thought-out proposal is that existing
code doesn't need to be changed. So let's say we have a template
function in a header file something like:
template<typename T>
void Func(T &&arg)
{
T var( std::forward<T>(arg) );
}
Imagine if we had a function that returns a string by value as follows:
extern string SomeLibraryFunc( int a, double b );
Now imagine if we could use a new keyword called 'prvalue' as follows:
int main(void)
{
prvalue x = SomeLibraryFunction( 8, 34.4 );
Func(x);
}
The compiler would have its work cut out for it here. Normally when
you write a function and give it a forwarding reference for a
parameter (i.e. T&&), the compiler only has to generate two real
functions (one that takes an Lvalue, one that takes an Rvalue). On
x86_64 Linux, the address of the argument would be passed in the RDI
register. But in the above code snippet, where you see the line
"Func(x);", the compiler would have to generate an implementation of
'Func' that receives a function pointer in RDI. This function pointer
would point to a function like this:
string CompilerGenerated000(void)
{
SomeLibraryFunction( 8, 34.4 );
}
And then within the implementation of "Func", this function pointer
would have to be invoked something like:
template<typename T>
void Func(T &&arg)
{
T var( arg() );
}
So the machine code for 'Func' accepts a function pointer in the RDI
register. The function pointer points to a function that returns the
PRalue, something like:
string CompilerGenerated000(void)
{
SomeLibraryFunction( 8, 34.4 );
}
and then this function pointer gets invoked in order to put the
PRvalue inside 'var' without the need for any copying or moving.
So let's look back at the following line:
prvalue x = SomeLibraryFunction( 8, 34.4 );
In the above line, which looks live a variable definition with
initialisation, we get some sort of thing called 'x'. 'x' is not a
variable in the sense that we already understand variables. It's more
like an 'invokable thing that can yield a PRvalue', almost like a kind
of 'vehicle' or 'promissory note' toward getting a PRvalue.
If we were able to create 'prvalue' thingies like this, we could then
pass them to template functions, and then effectively the template
function would be accepting a PRvalue as an argument. The best thing
about all of this is that none of the header files need to be edited
-- this new 'prvalue' feature could be used wiith pre-existing header
files that don't need to be changed.
I'm actually finished the post now -- I'm not keen on webmail sites
that try to interpret a few different hotkeys as 'Send'.
Received on 2025-10-19 22:24:33
