Why there is no std::construct_at in C++17?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
10
down vote

favorite
1












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);









share|improve this question

















  • 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, 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














up vote
10
down vote

favorite
1












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);









share|improve this question

















  • 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, 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












up vote
10
down vote

favorite
1









up vote
10
down vote

favorite
1






1





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);









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 35 mins ago









Daniel Langr

5,6272042




5,6272042







  • 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, 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












  • 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, 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







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












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 memory


  • uninitialized_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





share|improve this answer


















  • 2




    That's why C++ is less and less popular .... :(
    – shawn
    24 mins ago










  • No emplace-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 is uninitialized_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

















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






share|improve this answer




















  • There is also std::allocator_traits::destroy and still, there is std::destroy_at as well.
    – Daniel Langr
    20 mins ago










Your Answer





StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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 memory


  • uninitialized_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





share|improve this answer


















  • 2




    That's why C++ is less and less popular .... :(
    – shawn
    24 mins ago










  • No emplace-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 is uninitialized_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














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 memory


  • uninitialized_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





share|improve this answer


















  • 2




    That's why C++ is less and less popular .... :(
    – shawn
    24 mins ago










  • No emplace-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 is uninitialized_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












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 memory


  • uninitialized_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





share|improve this answer














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 memory


  • uninitialized_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






share|improve this answer














share|improve this answer



share|improve this answer








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










  • No emplace-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 is uninitialized_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




    That's why C++ is less and less popular .... :(
    – shawn
    24 mins ago










  • No emplace-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 is uninitialized_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












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






share|improve this answer




















  • There is also std::allocator_traits::destroy and still, there is std::destroy_at as well.
    – Daniel Langr
    20 mins ago














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






share|improve this answer




















  • There is also std::allocator_traits::destroy and still, there is std::destroy_at as well.
    – Daniel Langr
    20 mins ago












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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










answered 22 mins ago









jakub_d

5349




5349











  • 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















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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

Confectionery