Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9199187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:28:30+00:00 2026-06-17T22:28:30+00:00

I’m currently using aligned_storage to implement an ‘Optional’ type similar to that of boost::optional.

  • 0

I’m currently using aligned_storage to implement an ‘Optional’ type similar to that of boost::optional. To accomplish this I have a class member like so:

typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type t_;

I use placement new to create the object, however I don’t store the pointer returned anywhere. Instead, I access the underlying type of the object in all my member functions like this (obviously with checks to ensure the object is valid via a boolean flag also stored in my Optional type):

T const* operator->() const {
    return static_cast<T const*>(static_cast<void const*>(&t_));
}

My question is whether this is safe. My understanding is that my usage of placement new changes the ‘dynamic type’ of the object, and as long as I keep accessing the memory using that type I’ll be okay. However I’m not clear on whether I have to hold the pointer returned from the placement new or whether I’m allowed to just cast to the underlying type whenever I need to access it. I have read section 3.10 of the C++11 standard, however I’m not fluent enough in standardese to be sure.

If possible, I would feel better if you could give reference to the standard in your answer (it helps me sleep at night :P).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:28:32+00:00Added an answer on June 17, 2026 at 10:28 pm

    ABICT your use is safe.

    • Placement new of an object of type T will create an object starting at the address passed in.

    §5.3.4/10 says:

    A new-expression passes the amount of space requested to the
    allocation function as the first argument of type std::size_t. That
    argument shall be no less than the size of the object being created;
    it may be greater than the size of the object being created only if
    the object is an array.

    For a non-array object, the size allocated cannot be greater that the size of the object, so the object representation must start at the beginning of the allocated memory in order to fit.

    Placement new returns the pointer passed in (see § 18.6.1.3/2) as the result of the “allocation”, so the object representation of the constructed object will start at that address.

    • static_cast<> and implicit conversions between T* type and void* convert between a pointer to the object and a pointer to its storage, if the object is a complete object.

    §4.10/2 says:

    A prvalue of type “pointer to cv T,” where T is an object type, can be
    converted to a prvalue of type “pointer to cv void”. The result of
    converting a “pointer to cv T” to a “pointer to cv void” points to the
    start of the storage location where the object of type T resides, as
    if the object is a most derived object (1.8) of type T […]

    This defines the implicit conversion to convert as stated. Further §5.2.9[expr.static.cast]/4 defines static_cast<> for explicit conversions, where an implicit conversion exists to have the same effect as the implicit conversion:

    Otherwise, an expression e can be explicitly converted to a type T
    using a static_cast of the form static_cast<T>(e) if the declaration
    T t(e); is well-formed, for some invented temporary variable t (8.5).
    The effect of such an explicit conversion is the same as performing
    the declaration and initialization and then using the temporary
    variable as the result of the conversion. […]

    For the inverse static_cast<> (from void* to T*), §5.2.9/13 states:

    A prvalue of type “pointer to cv1 void” can be converted to a prvalue
    of type “pointer to cv2 T,” where T is an object type and cv2 is the
    same cv-qualification as, or greater cv-qualification than, cv1. […]
    A value of type pointer to object converted to “pointer to cv void”
    and back, possibly with different cv-qualification, shall have its
    original value.

    So if you have a void* pointing to the storage of the T object (which is the pointer value that would result from the implicit conversion of a T* to the object, then a static_cast of that to a T* will produce a valid pointer to the object.

    Returning to your question, the preceding points imply that if you have

    typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type t_;
    void * pvt_ = &t_;
    
    T* pT = new (&t_) T(args...);
    void * pvT = pT;
    

    then

    • the storage of *pT exactly overlays the first size(T) bytes of the storage of t_, so that pvT == pvt_
    • pvt_ == static_cast<void*>(&t_)
    • static_cast<T*>(pvT) == pT
    • Taken together that yields static_cast<T*>(static_cast<void*>(&t_)) == pT
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.