s***@gmail.com
2018-10-22 04:43:42 UTC
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.
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.
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.