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 8735895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:10:30+00:00 2026-06-13T10:10:30+00:00

Consider the following class: template <class T> class defer { public: template <class …Args>

  • 0

Consider the following class:

template <class T>
class defer {
public:
    template <class ...Args>
    void construct(Args&&...);
    T& obj();
    ~defer();
private:
    std::uint8_t memory[sizeof(T)];
    T * ptr();
};

template <class T>
template <class ...Args>
void defer<T>::construct(Args&& ...args) {
    new(static_cast<void*>(&memory[0])) T(std::forward<Args>(args)...);
}

template <class T>
T& defer<T>::obj() {
    return *(ptr());
}

template <class T>
defer<T>::~defer() {
    ptr()->~T();
}

template <class T>
T * defer<T>::ptr() {
    return static_cast<T*>(&memory[0]);
}

NOW I KNOW that there are issues with this, but in order to make the code short for purposes of discussion we are going to assume that the defer::construct() is always called before the object goes out of scope.

That being said, is it always necessarily safe to do this? Or can in some weird corner case of multiple virtual inheritance with other craziness can std::uint8_t[sizeof(T)] not allocate enough space?

  • 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-13T10:10:31+00:00Added an answer on June 13, 2026 at 10:10 am

    R. Martinho Fernandes beat me to it! Use

    typename std::aligned_storage<sizeof(T)>::type  memory;
    

    and you’re good to go. See here for details.


    As our panel of commentators has pointed out, the default alignment is always enough, but may be more stringent than required for your type (so you waste space with extra padding). You can avoid this by just specifying it explicitly:

    typename std::aligned_storage<sizeof(T), alignof(T)>::type memory;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: #include<iostream> #include<vector> using namespace std; class Foo { public: template<
Consider the following example: class MyContainer { std::vector<void *> v; public: void Put(void *x)
Consider the following class: class MyClass { public: template<class T> typename T::result_type apply(T& func)
Consider the following example template<class Type = void> class MyClass { public: double getValue()
Consider the following example: template <typename T> class A { public: void f() {
Consider the following: template <typename T> class testString { public: typedef T* iterator; void
Consider the following code: class A { public: virtual void f() throw ( int
Consider the following template class class MyClassInterface { public: virtual double foo(double) = 0;
consider the following class declarations: namespace X { template<class T> class Foo { public:
Consider the following code: #include <initializer_list> struct X{ static void init(const std::initializer_list<void*>& ptrs){} };

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.