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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:37:21+00:00 2026-05-26T20:37:21+00:00

class base { int a; protected: template<class T> class derived; public: base() {} virtual

  • 0
class base {
    int a;
protected:
    template<class T>
    class derived;
public:
    base() {}
    virtual ~base() {}
    virtual void func() {}
    static base* maker();
};

template <class T>    
class base::derived 
    : public base
{
public: 
    derived() {}
    virtual ~derived() {}
    virtual void func() {
        this->~derived(); //<--is this legal?
        new (this) derived<int>(); //<--is this legal?
    }
};

base* base::maker() {
    return new derived<double>();
}

int main() {
    base* p = base::maker(); //p is derivedA<double>
    p->func(); //p is now derivedA<int>
    delete p; //is the compiler allowed to call ~derived<double>()?
}

This is a Short, Self Contained, Correct (Compilable), Example of my code (which is basically reinventing any_iterator for my own growth).

The question reduces down to: is it undefined behavior to destroy this and reconstruct this with a different type virtually derived from the same base, when neither has any additional members over the shared base? Specifically, are compilers allowed to call keep track of the static type, or is that technically nonconforming?

[EDIT] Several people pointed out that compilers may call the incorrect destructor if derivedA is created on the stack. (1) I can’t find anything in the standard that allows compilers to do so. (2) That was aside from what I intended by my question, so I have changed the code to show that derived cannot be placed on the stack. base can still be on the stack though.

  • 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-05-26T20:37:22+00:00Added an answer on May 26, 2026 at 8:37 pm

    I think that’s clearly not OK.

    As a preface, and object’s lifetime can indeed be ended by calling the destructor, but you’re only allowed (and required) to construct a new object of the same type in its place:

    {
      Foo x;
      x.~Foo();
      ::new (&x) Foo;
    }  // x.~Foo() must be a valid call here!
    

    Remember that at scope’s end the destructor will be invoked!

    But in your case you’re constructing an entirely different object:

    ::new (&x) Bar;   // Bar = DerivedA<int>
    

    Clearly if sizeof(Bar) exceeds sizeof(Foo) this cannot be OK.

    (Perhaps if you can make additional guarantees on the object sizes, as well as on the alignment guarantees, we can think about this further.)

    Update: Even if you’re thinking, OK, so those types are derived from the same base, so invoking the destructor brings virtual happiness, I’m still pretty sure that that’s a violation. In this static setting, the compiler may well resolve the virtual call statically, so you’re breaking the compiler’s assumptions if you change the dynamic type of the thing pointed to by &x.

    Update 2: Another thought on the same matter: The static type of *&x is known, and I think you have to respect that. In other words, the compiler has no reason to factor in the possibility that the static type of a local variable changes.

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

Sidebar

Related Questions

class Base{ public: void setX(int a) {x=a;} private: int x; }; class D1: protected
class Base { protected: int data; public: virtual int getData() { return data; }
class Base { public: Base(){} Base(int k):a(k) { } int a; }; class X:virtual
Is this scenario even possible? class Base { int someBaseMemer; }; template<class T> class
Let's say we have these two classes: public class Base { public static int
I have a base class like this: public class BaseModalCommand { protected object m_commandArgument;
I have a class like: Base.h class Base { public: Base(); virtual int getInfo(int
#include<iostream> using namespace std; class base { protected: int a; public: base(int i) {
I created some class: class Base{ public: Base(int = 0); ~Base(); Base(Base&); Base(Derived&); //<-
class Base { private: int var; }; class Child : protected Base { void

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.