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

  • Home
  • SEARCH
  • 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 8802961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:12:57+00:00 2026-06-14T01:12:57+00:00

#include <iostream> #include <new> #include <type_traits> template<typename T, typename U> struct promote { typedef

  • 0
#include <iostream>
#include <new>
#include <type_traits>

template<typename T, typename U>
struct promote
{
  typedef typename std::conditional<(sizeof(T) > sizeof(U)), T, U>::type type;
};    

template<class U, class V>
class risk_implementation
{
  public:

  template<class T>
  risk_implementation(T const &t)      
  {
    new(storage_) T(t);
  }      

 //easier to do some test with public
 typedef typename promote<U, V>::type Bigger;
 typedef typename std::aligned_storage<sizeof(Bigger), alignof(Bigger)>::type storage_type;
 storage_type storage_[1];           
};

This kind of implementation is ugly, and I would not use it in the real case
I only want to know using placement new like this are safe or not?Thanks

Thanks to both of you, I alter the codes a little bit, is this safe now?

  • 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-14T01:12:59+00:00Added an answer on June 14, 2026 at 1:12 am

    Not necessarily, because of alignment issues your code may fail, storage_ member must be aligned in a way that can handle alignment of both U and V, so it is not always safe

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> using namespace std; struct testarray{ int element; public: testarray(int a):element(a){} }; class
If I have this kind of hierarchy: #include <iostream> using namespace std; template<class T>
#include<iostream> #include<stdlib.h> #include<string.h> #include<stdio.h> using namespace std; union type{ int a; char b; int
#include <iostream> using namespace std; // This first class contains a vector and a
Ok heres some code. #include <iostream> #include <deque> using namespace std; class A {
#include<iostream> using namespace std; class StudentClass{ friend class Oopclass; public: StudentClass(); void setStudentData(); void
$ cat inheritance.cpp #include <iostream> using namespace std; class A { }; class B
I'm trying to understand why this snippet fails: #include <iostream> using namespace std; template
Consider the example below: #include <iostream> using namespace std; class base { public: virtual
1st code: #include <iostream> using namespace std; class demo { int a; public: demo():a(9){}

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.