Discussion:
[std-proposals] Use new keyword to indicate that a function should return it's argument allocated on the heap
s***@gmail.com
2018-10-22 04:43:42 UTC
Permalink
We could write a function to take something allocated on the stack and
return a copy of it on the heap:

template<class T> T* as_new(T&& obj) {
return new T{ std::forward<decltype(obj)>(obj)};
}

And this works in most cases while only incurring a performance hit.
However, it breaks down when using objects that either can't be copied or
moved. Providing overloads of functions that allocate something in a
specific provided space can be tedious, too.

*Proposal: *putting the new keyword in parethesis before a function or
expression results in the output of that expression being allocated on the
heap, or in the provided buffer.

Let's take the function make_tuple as an example:

#include <tuple>
int main() {
using std::tuple;
using std::make_tuple;

/* Allocates tuple normally */
tuple<int, double> t1 = make_tuple(4, 0.3);

/*Allocates tuple on heap, returns pointer */
tuple<int, double>* t2 = (new) make_tuple(4, 0.3);

char buff[100];

/* Allocates tuple in buff, returns pointer */
tuple<int, double>* t3 = (new(buff)) make_tuple(4, 0.3);
}


if a function returns a reference, it's a compiler error to use this
construct, and if the function returns a pointer, then this construct
returns a pointer to that pointer.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/269cd5f8-0201-431b-9683-52a945aa9c3d%40isocpp.org.
m***@gmail.com
2018-10-22 08:23:58 UTC
Permalink
You must give better motivation.
If you want to use a function that returns an object and have that object
new-ed without copy/move, then you should explain what is stopping you
creating the returned object via the new expression in the first place,
especially now with template deduction guide.
If you want a copy/move of the object, returned by the function you can do
this today using new auto(create_object());

My guess is you want the former, but some better examples would be nice.
Post by s***@gmail.com
We could write a function to take something allocated on the stack and
template<class T> T* as_new(T&& obj) {
return new T{ std::forward<decltype(obj)>(obj)};
}
And this works in most cases while only incurring a performance hit.
However, it breaks down when using objects that either can't be copied or
moved. Providing overloads of functions that allocate something in a
specific provided space can be tedious, too.
*Proposal: *putting the new keyword in parethesis before a function or
expression results in the output of that expression being allocated on the
heap, or in the provided buffer.
#include <tuple>
int main() {
using std::tuple;
using std::make_tuple;
/* Allocates tuple normally */
tuple<int, double> t1 = make_tuple(4, 0.3);
/*Allocates tuple on heap, returns pointer */
tuple<int, double>* t2 = (new) make_tuple(4, 0.3);
char buff[100];
/* Allocates tuple in buff, returns pointer */
tuple<int, double>* t3 = (new(buff)) make_tuple(4, 0.3);
}
if a function returns a reference, it's a compiler error to use this
construct, and if the function returns a pointer, then this construct
returns a pointer to that pointer.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/c5e8d685-d72c-4163-bb6d-f9cee0fa190b%40isocpp.org.
Richard Smith
2018-10-23 02:30:36 UTC
Permalink
Post by m***@gmail.com
You must give better motivation.
If you want to use a function that returns an object and have that object
new-ed without copy/move, then you should explain what is stopping you
creating the returned object via the new expression in the first place,
especially now with template deduction guide.
If you want a copy/move of the object, returned by the function you can do
this today using new auto(create_object());
If create_object() returns by value (as make_tuple does, for instance),
this does not perform a copy or move. Rather, it constructs directly into
the allocated storage. That is, we already have the feature that is being
requested here: just use 'new'.

My guess is you want the former, but some better examples would be nice.
Post by m***@gmail.com
Post by s***@gmail.com
We could write a function to take something allocated on the stack and
template<class T> T* as_new(T&& obj) {
return new T{ std::forward<decltype(obj)>(obj)};
}
And this works in most cases while only incurring a performance hit.
However, it breaks down when using objects that either can't be copied or
moved. Providing overloads of functions that allocate something in a
specific provided space can be tedious, too.
*Proposal: *putting the new keyword in parethesis before a function or
expression results in the output of that expression being allocated on the
heap, or in the provided buffer.
#include <tuple>
int main() {
using std::tuple;
using std::make_tuple;
/* Allocates tuple normally */
tuple<int, double> t1 = make_tuple(4, 0.3);
/*Allocates tuple on heap, returns pointer */
tuple<int, double>* t2 = (new) make_tuple(4, 0.3);
char buff[100];
/* Allocates tuple in buff, returns pointer */
tuple<int, double>* t3 = (new(buff)) make_tuple(4, 0.3);
}
if a function returns a reference, it's a compiler error to use this
construct, and if the function returns a pointer, then this construct
returns a pointer to that pointer.
--
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/c5e8d685-d72c-4163-bb6d-f9cee0fa190b%40isocpp.org
<https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/c5e8d685-d72c-4163-bb6d-f9cee0fa190b%40isocpp.org?utm_medium=email&utm_source=footer>
.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAOfiQqnyH1r5e5QEKEgeSfa0pg3LbcVDb-URGLgGLap2f%2BJ68A%40mail.gmail.com.
m***@gmail.com
2018-10-23 07:16:18 UTC
Permalink
Post by Richard Smith
Post by m***@gmail.com
You must give better motivation.
If you want to use a function that returns an object and have that object
new-ed without copy/move, then you should explain what is stopping you
creating the returned object via the new expression in the first place,
especially now with template deduction guide.
If you want a copy/move of the object, returned by the function you can
do this today using new auto(create_object());
If create_object() returns by value (as make_tuple does, for instance),
this does not perform a copy or move. Rather, it constructs directly into
the allocated storage. That is, we already have the feature that is being
requested here: just use 'new'.
Great to hear! It didn't know the new RVO rules work here as well as it is
not so obvious - we tend to think this is stack-only. Very interesting...
I might as well update cppreference to note that it works here as well.
Post by Richard Smith
My guess is you want the former, but some better examples would be nice.
Post by m***@gmail.com
Post by s***@gmail.com
We could write a function to take something allocated on the stack and
template<class T> T* as_new(T&& obj) {
return new T{ std::forward<decltype(obj)>(obj)};
}
And this works in most cases while only incurring a performance hit.
However, it breaks down when using objects that either can't be copied or
moved. Providing overloads of functions that allocate something in a
specific provided space can be tedious, too.
*Proposal: *putting the new keyword in parethesis before a function or
expression results in the output of that expression being allocated on the
heap, or in the provided buffer.
#include <tuple>
int main() {
using std::tuple;
using std::make_tuple;
/* Allocates tuple normally */
tuple<int, double> t1 = make_tuple(4, 0.3);
/*Allocates tuple on heap, returns pointer */
tuple<int, double>* t2 = (new) make_tuple(4, 0.3);
char buff[100];
/* Allocates tuple in buff, returns pointer */
tuple<int, double>* t3 = (new(buff)) make_tuple(4, 0.3);
}
if a function returns a reference, it's a compiler error to use this
construct, and if the function returns a pointer, then this construct
returns a pointer to that pointer.
--
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/c5e8d685-d72c-4163-bb6d-f9cee0fa190b%40isocpp.org
<https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/c5e8d685-d72c-4163-bb6d-f9cee0fa190b%40isocpp.org?utm_medium=email&utm_source=footer>
.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/bdbf2214-87c0-4d14-8831-c6ffd5da2eba%40isocpp.org.
Loading...