Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qualified call to new and cast placement param to void* to ensure placement new #9

Closed
wants to merge 1 commit into from

Conversation

EricWF
Copy link

@EricWF EricWF commented Mar 15, 2014

I may be mistaken, but it would seem that placement new is desired. The changes made in this pull request are suggested by STL to ensure placement new is called and not an overload.
http://www.reddit.com/r/cpp/comments/1tedni/notsocommon_pitfalls_of_c_xpost_rcplusplus/ce7bqnu

@akrzemi1
Copy link
Owner

I have some doubts about this change. If a user overloads operator new in this way, is it not because he wants it to be used, even in libraries like tr2::optional?

@EricWF
Copy link
Author

EricWF commented Apr 25, 2014

True placement new cannot be overloaded.

§18.6.1.3 Placement forms [new.delete.placement]
1 These functions are reserved, a C++ program may not define functions that displace the versions in the Standard C++ library (17.6.4). The provisions of (3.7.4) do not apply to these reserved placement forms of operator new and operator delete.

However, the user can provide a overload for the allocating version of new. (As far as I can tell, the standard requires that every overload of new allocates memory). Since the signature of True placement new is:

void* operator new(std::size_t size, void* ptr) noexcept;

It would be legal however for a user to provide an overload of new with the following signature

void* operator new(std::size_t size, T* ptr);

where T* ptr is a placement parameter.

Currently, optional will call the user provided overload, which is obviously not what we want.
In short the user cannot actually overload placement new (legally), so we want to make sure we are calling true placement new.

Anecdotally every example of placement new I've come across in the standard is of the form

::new ((void*)c) C(std::forward<Args>(args)...);

(ie. It is scoped, and has the void cast). The most convincing of which is the default definition of construct given in the specification of Allocator requirements § 17.6.3.5.

Please let me know if I have made any mistakes or if you have any questions.

@akrzemi1
Copy link
Owner

akrzemi1 commented May 8, 2014

I applied your suggestion. Thanks.

@akrzemi1 akrzemi1 closed this May 8, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants