+3  A: 

When the following runs

test();

you construct a temporary (new) object that is immediately destroyed when control "passes by the semicolon", the destructor for that temporary object is invoked, which constructs another temporary object, etc., so you get a death spiral of endless recursive calls which leads to a stack overflow and crashes your program.

Prohibiting the destructor from creating temporary objects would be ridiculous - it would severely limit you in what code you could right. Also it makes no sense - the destructor is destroying the current object, and those temporary object are completely irrelevant to it, so enforcing such constrains on them is meaningless.

sharptooth
but there is an infinite recursion, right? the temporary object is destroyed, which creates a temporary object, which is destroyed, ... no?
aioobe
@sharptooth: Correct, I learned this something from you in my last post , but this time my question in why compiler doesn't complain when there is nothing concrete that we can do here in DTOR.
dicaprio
@dicaprio: It doesn't compplain, because that code is completely legal. You can legally create any temporary objects while your destructor is running.
sharptooth
@aioobe: Yes, that's infinite recursion.
sharptooth
Ok, I have to agree halfheartedly that it is legal to do this, but then is there any real use ? Do you have any real example which can support. What I'm trying to understand is, if they are supporting something and which is legal , then it should be of good use? what is that?
dicaprio
@dicaprio: Infinite recursion is unlikely to ever be useful. Unless there's a non-recursive branch that stops it at some point of depth you'll run into stack overflow anyway.
sharptooth
Oh! I got your point, recursion was not in my mind, I did see the result and it goes into infinite loops and yes it make sense there, but i keep stressing on why DTOR has to support creating object when it actually meant for cleaning up, thats my point.
dicaprio
@dicaprio: I expanded my answer.
sharptooth
+1  A: 

As far as I understand you're simply instantiate new test object in the destructor and leave it intact.

nailxx
A: 

Static analysis tools are the things which should complain. For me your case is not very different from the following:

void foo();
void bar()
{
   foo();
}
void foo()
{
   bar();
}

I don't know are there any compilers which will complain about above code, but this example is much simpler than yours and there can be many others.

EDIT: In your case the problem is much simpler. It's an ordinary infinite recursion, because the idea of your destructor is somewhat like that:

   ~test()
   {
      cout<<"DTOR"<<endl;
      test tmp();
      tmp.~test(); // infinite recursion.
   }
Dmitriy Matveev
Absolutely simple and good example, thanks. I haven't tried this ever.
dicaprio
A: 

I see no reason why it should be illegal, but I admit I'm struggling to come up with a decent example of why I'd do this. To make it "work" you'd need to conditionally call the c'tor rather than unconditionally.

Stephen Nutt
+1  A: 

This code, which gives the test instances a large size, actually produces a stack overflow very quickly, because of the infinite recursion:

#include <iostream>
using namespace std;
class test{
   public:
    int a[10000];
    test(){
     }

~test() {
 test();
 }};

int main() {
    test t;
}

C++ does not require that a warning be issued for infinite recursion, and in general it is very difficult to detect.

anon
Neil , I remember you said earlier in response to me comments that , compilers emits extra stuff when a CTOR/DTOR is called, when in this case it seems is absolutely unnecessary, I believe it should have been avoid if there is no real use.
dicaprio
@dicaprio A compiler could optimise away the call, or there again it could choose not to. Just because you can see the call is pointless does not mean that the compiler can.
anon
@dicaprio: Also the code is not meaningless - it asks the compiler to construct a temporary object that has a non-trivial destructor, so the compiler just does that.
sharptooth
@dicaprio In fact with -O3 optimisation enabled, g++ does optimise away the constructor call and the program runs without a stack overflow.
anon