Why there is no std::construct_at in C++17?
Clash Royale CLAN TAG#URR8PPP
up vote
10
down vote
favorite
C++17 adds std::destroy_at
, but there is no std::construct_at
counterpart. Why is that? Couldn't it be implemented as simply as:
template <typename T, typename... Args>
void construct_at(T* addr, Args&&... args)
new (addr) T(std::forward<Args>(args)...);
Which would enable to avoid that not-entirely-natural placement new syntax:
int* ptr = (int*)::operator new(sizeof(int));
construct_at<int>(ptr, 1); // instead of 'new (ptr) int(1);'
std::cout << *ptr;
std::destroy_at(ptr);
::operator delete(ptr);
c++ c++17 placement-new
 |Â
show 4 more comments
up vote
10
down vote
favorite
C++17 adds std::destroy_at
, but there is no std::construct_at
counterpart. Why is that? Couldn't it be implemented as simply as:
template <typename T, typename... Args>
void construct_at(T* addr, Args&&... args)
new (addr) T(std::forward<Args>(args)...);
Which would enable to avoid that not-entirely-natural placement new syntax:
int* ptr = (int*)::operator new(sizeof(int));
construct_at<int>(ptr, 1); // instead of 'new (ptr) int(1);'
std::cout << *ptr;
std::destroy_at(ptr);
::operator delete(ptr);
c++ c++17 placement-new
1
in my ignorance i rather wonder whatdestroy_at
is good for ;)
– user463035818
30 mins ago
3
@user463035818 - How would one do a pseudo destructor call for astd::string
object? Don't get me wrong, I'm not saying it's impossible, I just want you to imagine the required expression vividly.
– StoryTeller
29 mins ago
@user463035818 this is useful when implementing advanced low level stuff like for example std::any or std::optional. In everyday code you will not use that.
– Marek R
12 mins ago
@StoryTeller Let me be another one naive, but what is wrong withauto s = new string"test"; s->~string();
? Am I missing premise of the question? I guess calling pseudo destructors on compound template types would be syntax nightmare, buttypedef
suffices. Isdestroy_at
solving same problems asmake_*
wrappers?
– luk32
10 mins ago
@luk32 - Try it without a using declaration in sight. Fully qualifystd::string
.
– StoryTeller
5 mins ago
 |Â
show 4 more comments
up vote
10
down vote
favorite
up vote
10
down vote
favorite
C++17 adds std::destroy_at
, but there is no std::construct_at
counterpart. Why is that? Couldn't it be implemented as simply as:
template <typename T, typename... Args>
void construct_at(T* addr, Args&&... args)
new (addr) T(std::forward<Args>(args)...);
Which would enable to avoid that not-entirely-natural placement new syntax:
int* ptr = (int*)::operator new(sizeof(int));
construct_at<int>(ptr, 1); // instead of 'new (ptr) int(1);'
std::cout << *ptr;
std::destroy_at(ptr);
::operator delete(ptr);
c++ c++17 placement-new
C++17 adds std::destroy_at
, but there is no std::construct_at
counterpart. Why is that? Couldn't it be implemented as simply as:
template <typename T, typename... Args>
void construct_at(T* addr, Args&&... args)
new (addr) T(std::forward<Args>(args)...);
Which would enable to avoid that not-entirely-natural placement new syntax:
int* ptr = (int*)::operator new(sizeof(int));
construct_at<int>(ptr, 1); // instead of 'new (ptr) int(1);'
std::cout << *ptr;
std::destroy_at(ptr);
::operator delete(ptr);
c++ c++17 placement-new
c++ c++17 placement-new
asked 35 mins ago
Daniel Langr
5,6272042
5,6272042
1
in my ignorance i rather wonder whatdestroy_at
is good for ;)
– user463035818
30 mins ago
3
@user463035818 - How would one do a pseudo destructor call for astd::string
object? Don't get me wrong, I'm not saying it's impossible, I just want you to imagine the required expression vividly.
– StoryTeller
29 mins ago
@user463035818 this is useful when implementing advanced low level stuff like for example std::any or std::optional. In everyday code you will not use that.
– Marek R
12 mins ago
@StoryTeller Let me be another one naive, but what is wrong withauto s = new string"test"; s->~string();
? Am I missing premise of the question? I guess calling pseudo destructors on compound template types would be syntax nightmare, buttypedef
suffices. Isdestroy_at
solving same problems asmake_*
wrappers?
– luk32
10 mins ago
@luk32 - Try it without a using declaration in sight. Fully qualifystd::string
.
– StoryTeller
5 mins ago
 |Â
show 4 more comments
1
in my ignorance i rather wonder whatdestroy_at
is good for ;)
– user463035818
30 mins ago
3
@user463035818 - How would one do a pseudo destructor call for astd::string
object? Don't get me wrong, I'm not saying it's impossible, I just want you to imagine the required expression vividly.
– StoryTeller
29 mins ago
@user463035818 this is useful when implementing advanced low level stuff like for example std::any or std::optional. In everyday code you will not use that.
– Marek R
12 mins ago
@StoryTeller Let me be another one naive, but what is wrong withauto s = new string"test"; s->~string();
? Am I missing premise of the question? I guess calling pseudo destructors on compound template types would be syntax nightmare, buttypedef
suffices. Isdestroy_at
solving same problems asmake_*
wrappers?
– luk32
10 mins ago
@luk32 - Try it without a using declaration in sight. Fully qualifystd::string
.
– StoryTeller
5 mins ago
1
1
in my ignorance i rather wonder what
destroy_at
is good for ;)– user463035818
30 mins ago
in my ignorance i rather wonder what
destroy_at
is good for ;)– user463035818
30 mins ago
3
3
@user463035818 - How would one do a pseudo destructor call for a
std::string
object? Don't get me wrong, I'm not saying it's impossible, I just want you to imagine the required expression vividly.– StoryTeller
29 mins ago
@user463035818 - How would one do a pseudo destructor call for a
std::string
object? Don't get me wrong, I'm not saying it's impossible, I just want you to imagine the required expression vividly.– StoryTeller
29 mins ago
@user463035818 this is useful when implementing advanced low level stuff like for example std::any or std::optional. In everyday code you will not use that.
– Marek R
12 mins ago
@user463035818 this is useful when implementing advanced low level stuff like for example std::any or std::optional. In everyday code you will not use that.
– Marek R
12 mins ago
@StoryTeller Let me be another one naive, but what is wrong with
auto s = new string"test"; s->~string();
? Am I missing premise of the question? I guess calling pseudo destructors on compound template types would be syntax nightmare, but typedef
suffices. Is destroy_at
solving same problems as make_*
wrappers?– luk32
10 mins ago
@StoryTeller Let me be another one naive, but what is wrong with
auto s = new string"test"; s->~string();
? Am I missing premise of the question? I guess calling pseudo destructors on compound template types would be syntax nightmare, but typedef
suffices. Is destroy_at
solving same problems as make_*
wrappers?– luk32
10 mins ago
@luk32 - Try it without a using declaration in sight. Fully qualify
std::string
.– StoryTeller
5 mins ago
@luk32 - Try it without a using declaration in sight. Fully qualify
std::string
.– StoryTeller
5 mins ago
 |Â
show 4 more comments
2 Answers
2
active
oldest
votes
up vote
5
down vote
There is such thing, but is named in different way then your expectation:
uninitialized_copy
copies a range of objects to an uninitialized area of memoryuninitialized_copy_n
(C++11)
copies a number of objects to an uninitialized area of memory
(function template)uninitialized_fill
copies an object to an uninitialized area of memory, defined by a range
(function template)
uninitialized_fill_n
copies an object to an uninitialized area of memory, defined by a start and a count
(function template)
uninitialized_move
(C++17)
moves a range of objects to an uninitialized area of memory
(function template)
uninitialized_move_n
(C++17)
moves a number of objects to an uninitialized area of memory
(function template)
uninitialized_default_construct
(C++17)
constructs objects by default-initialization in an uninitialized area of memory, defined by a range
(function template)
uninitialized_default_construct_n
(C++17)
constructs objects by default-initialization in an uninitialized area of memory, defined by a start and a count
(function template)
uninitialized_value_construct
(C++17)
constructs objects by value-initialization in an uninitialized area of memory, defined by a range
(function template)
uninitialized_value_construct_n
(C++17)
constructs objects by value-initialization in an uninitialized area of memory, defined by a start and a count
2
That's why C++ is less and less popular .... :(
– shawn
24 mins ago
Noemplace
-style parameter-forwarding function though.
– Quentin
23 mins ago
1
Is any of these functions able to forward arbitrary arguments to constructor?
– Daniel Langr
23 mins ago
there isuninitialized_move
on the list above.
– Marek R
18 mins ago
1
@shawn eh... this is stuff you don't need for normal coding and other language don't even do
– M.M
11 mins ago
 |Â
show 1 more comment
up vote
3
down vote
There is std::allocator_traits::construct - see https://en.cppreference.com/w/cpp/memory/allocator_traits/construct . There used to be one more in std::allocator, but that got removed, rationale in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0174r0.html#2.4
There is alsostd::allocator_traits::destroy
and still, there isstd::destroy_at
as well.
– Daniel Langr
20 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
There is such thing, but is named in different way then your expectation:
uninitialized_copy
copies a range of objects to an uninitialized area of memoryuninitialized_copy_n
(C++11)
copies a number of objects to an uninitialized area of memory
(function template)uninitialized_fill
copies an object to an uninitialized area of memory, defined by a range
(function template)
uninitialized_fill_n
copies an object to an uninitialized area of memory, defined by a start and a count
(function template)
uninitialized_move
(C++17)
moves a range of objects to an uninitialized area of memory
(function template)
uninitialized_move_n
(C++17)
moves a number of objects to an uninitialized area of memory
(function template)
uninitialized_default_construct
(C++17)
constructs objects by default-initialization in an uninitialized area of memory, defined by a range
(function template)
uninitialized_default_construct_n
(C++17)
constructs objects by default-initialization in an uninitialized area of memory, defined by a start and a count
(function template)
uninitialized_value_construct
(C++17)
constructs objects by value-initialization in an uninitialized area of memory, defined by a range
(function template)
uninitialized_value_construct_n
(C++17)
constructs objects by value-initialization in an uninitialized area of memory, defined by a start and a count
2
That's why C++ is less and less popular .... :(
– shawn
24 mins ago
Noemplace
-style parameter-forwarding function though.
– Quentin
23 mins ago
1
Is any of these functions able to forward arbitrary arguments to constructor?
– Daniel Langr
23 mins ago
there isuninitialized_move
on the list above.
– Marek R
18 mins ago
1
@shawn eh... this is stuff you don't need for normal coding and other language don't even do
– M.M
11 mins ago
 |Â
show 1 more comment
up vote
5
down vote
There is such thing, but is named in different way then your expectation:
uninitialized_copy
copies a range of objects to an uninitialized area of memoryuninitialized_copy_n
(C++11)
copies a number of objects to an uninitialized area of memory
(function template)uninitialized_fill
copies an object to an uninitialized area of memory, defined by a range
(function template)
uninitialized_fill_n
copies an object to an uninitialized area of memory, defined by a start and a count
(function template)
uninitialized_move
(C++17)
moves a range of objects to an uninitialized area of memory
(function template)
uninitialized_move_n
(C++17)
moves a number of objects to an uninitialized area of memory
(function template)
uninitialized_default_construct
(C++17)
constructs objects by default-initialization in an uninitialized area of memory, defined by a range
(function template)
uninitialized_default_construct_n
(C++17)
constructs objects by default-initialization in an uninitialized area of memory, defined by a start and a count
(function template)
uninitialized_value_construct
(C++17)
constructs objects by value-initialization in an uninitialized area of memory, defined by a range
(function template)
uninitialized_value_construct_n
(C++17)
constructs objects by value-initialization in an uninitialized area of memory, defined by a start and a count
2
That's why C++ is less and less popular .... :(
– shawn
24 mins ago
Noemplace
-style parameter-forwarding function though.
– Quentin
23 mins ago
1
Is any of these functions able to forward arbitrary arguments to constructor?
– Daniel Langr
23 mins ago
there isuninitialized_move
on the list above.
– Marek R
18 mins ago
1
@shawn eh... this is stuff you don't need for normal coding and other language don't even do
– M.M
11 mins ago
 |Â
show 1 more comment
up vote
5
down vote
up vote
5
down vote
There is such thing, but is named in different way then your expectation:
uninitialized_copy
copies a range of objects to an uninitialized area of memoryuninitialized_copy_n
(C++11)
copies a number of objects to an uninitialized area of memory
(function template)uninitialized_fill
copies an object to an uninitialized area of memory, defined by a range
(function template)
uninitialized_fill_n
copies an object to an uninitialized area of memory, defined by a start and a count
(function template)
uninitialized_move
(C++17)
moves a range of objects to an uninitialized area of memory
(function template)
uninitialized_move_n
(C++17)
moves a number of objects to an uninitialized area of memory
(function template)
uninitialized_default_construct
(C++17)
constructs objects by default-initialization in an uninitialized area of memory, defined by a range
(function template)
uninitialized_default_construct_n
(C++17)
constructs objects by default-initialization in an uninitialized area of memory, defined by a start and a count
(function template)
uninitialized_value_construct
(C++17)
constructs objects by value-initialization in an uninitialized area of memory, defined by a range
(function template)
uninitialized_value_construct_n
(C++17)
constructs objects by value-initialization in an uninitialized area of memory, defined by a start and a count
There is such thing, but is named in different way then your expectation:
uninitialized_copy
copies a range of objects to an uninitialized area of memoryuninitialized_copy_n
(C++11)
copies a number of objects to an uninitialized area of memory
(function template)uninitialized_fill
copies an object to an uninitialized area of memory, defined by a range
(function template)
uninitialized_fill_n
copies an object to an uninitialized area of memory, defined by a start and a count
(function template)
uninitialized_move
(C++17)
moves a range of objects to an uninitialized area of memory
(function template)
uninitialized_move_n
(C++17)
moves a number of objects to an uninitialized area of memory
(function template)
uninitialized_default_construct
(C++17)
constructs objects by default-initialization in an uninitialized area of memory, defined by a range
(function template)
uninitialized_default_construct_n
(C++17)
constructs objects by default-initialization in an uninitialized area of memory, defined by a start and a count
(function template)
uninitialized_value_construct
(C++17)
constructs objects by value-initialization in an uninitialized area of memory, defined by a range
(function template)
uninitialized_value_construct_n
(C++17)
constructs objects by value-initialization in an uninitialized area of memory, defined by a start and a count
edited 17 mins ago
answered 25 mins ago
Marek R
12.1k22669
12.1k22669
2
That's why C++ is less and less popular .... :(
– shawn
24 mins ago
Noemplace
-style parameter-forwarding function though.
– Quentin
23 mins ago
1
Is any of these functions able to forward arbitrary arguments to constructor?
– Daniel Langr
23 mins ago
there isuninitialized_move
on the list above.
– Marek R
18 mins ago
1
@shawn eh... this is stuff you don't need for normal coding and other language don't even do
– M.M
11 mins ago
 |Â
show 1 more comment
2
That's why C++ is less and less popular .... :(
– shawn
24 mins ago
Noemplace
-style parameter-forwarding function though.
– Quentin
23 mins ago
1
Is any of these functions able to forward arbitrary arguments to constructor?
– Daniel Langr
23 mins ago
there isuninitialized_move
on the list above.
– Marek R
18 mins ago
1
@shawn eh... this is stuff you don't need for normal coding and other language don't even do
– M.M
11 mins ago
2
2
That's why C++ is less and less popular .... :(
– shawn
24 mins ago
That's why C++ is less and less popular .... :(
– shawn
24 mins ago
No
emplace
-style parameter-forwarding function though.– Quentin
23 mins ago
No
emplace
-style parameter-forwarding function though.– Quentin
23 mins ago
1
1
Is any of these functions able to forward arbitrary arguments to constructor?
– Daniel Langr
23 mins ago
Is any of these functions able to forward arbitrary arguments to constructor?
– Daniel Langr
23 mins ago
there is
uninitialized_move
on the list above.– Marek R
18 mins ago
there is
uninitialized_move
on the list above.– Marek R
18 mins ago
1
1
@shawn eh... this is stuff you don't need for normal coding and other language don't even do
– M.M
11 mins ago
@shawn eh... this is stuff you don't need for normal coding and other language don't even do
– M.M
11 mins ago
 |Â
show 1 more comment
up vote
3
down vote
There is std::allocator_traits::construct - see https://en.cppreference.com/w/cpp/memory/allocator_traits/construct . There used to be one more in std::allocator, but that got removed, rationale in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0174r0.html#2.4
There is alsostd::allocator_traits::destroy
and still, there isstd::destroy_at
as well.
– Daniel Langr
20 mins ago
add a comment |Â
up vote
3
down vote
There is std::allocator_traits::construct - see https://en.cppreference.com/w/cpp/memory/allocator_traits/construct . There used to be one more in std::allocator, but that got removed, rationale in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0174r0.html#2.4
There is alsostd::allocator_traits::destroy
and still, there isstd::destroy_at
as well.
– Daniel Langr
20 mins ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
There is std::allocator_traits::construct - see https://en.cppreference.com/w/cpp/memory/allocator_traits/construct . There used to be one more in std::allocator, but that got removed, rationale in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0174r0.html#2.4
There is std::allocator_traits::construct - see https://en.cppreference.com/w/cpp/memory/allocator_traits/construct . There used to be one more in std::allocator, but that got removed, rationale in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0174r0.html#2.4
answered 22 mins ago
jakub_d
5349
5349
There is alsostd::allocator_traits::destroy
and still, there isstd::destroy_at
as well.
– Daniel Langr
20 mins ago
add a comment |Â
There is alsostd::allocator_traits::destroy
and still, there isstd::destroy_at
as well.
– Daniel Langr
20 mins ago
There is also
std::allocator_traits::destroy
and still, there is std::destroy_at
as well.– Daniel Langr
20 mins ago
There is also
std::allocator_traits::destroy
and still, there is std::destroy_at
as well.– Daniel Langr
20 mins ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52966560%2fwhy-there-is-no-stdconstruct-at-in-c17%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
in my ignorance i rather wonder what
destroy_at
is good for ;)– user463035818
30 mins ago
3
@user463035818 - How would one do a pseudo destructor call for a
std::string
object? Don't get me wrong, I'm not saying it's impossible, I just want you to imagine the required expression vividly.– StoryTeller
29 mins ago
@user463035818 this is useful when implementing advanced low level stuff like for example std::any or std::optional. In everyday code you will not use that.
– Marek R
12 mins ago
@StoryTeller Let me be another one naive, but what is wrong with
auto s = new string"test"; s->~string();
? Am I missing premise of the question? I guess calling pseudo destructors on compound template types would be syntax nightmare, buttypedef
suffices. Isdestroy_at
solving same problems asmake_*
wrappers?– luk32
10 mins ago
@luk32 - Try it without a using declaration in sight. Fully qualify
std::string
.– StoryTeller
5 mins ago