22

I have come across the following piece of code and i couldn't understand what it means:

typedef int INT; 
5 .INT::~INT();

Note: There is a space between the numeric 5 and decimal.

Questions: 1. Could somebody explain what exactly does the two lines above mean? 2. Why would it not work without typedef? Any reason behind it?5 .int::~int() throws error.

anurag86
  • 1,635
  • 1
  • 16
  • 31

1 Answers1

16

.INT::~INT() is a pseudo destructor call, useful for templated code.

Note that it can't be used without the typedef type.


Standardese:

C++03 §5.2.4 “Pseudo destructor call”:
  1. The use of a pseudo-destructor-name after a dot . or arrow -> operator represents the destructor for the non-class type named by type-name. The result shall only be used as the operand for the function call operator (), and the result of such a call has type void. The only effect is the evaluation of the postfix-expression before the dot or arrow.
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 2
    But it is pointless in the given example, right? Can you elaborate what it could be possibly meant to here? – jpo38 Sep 19 '15 at 13:41
  • 2
    It's just a no-op. There is no cleanup for an `int`. – Cheers and hth. - Alf Sep 19 '15 at 13:42
  • It's probably even undefined behavior in the example, given that `5` is a constant expression –  Sep 19 '15 at 13:56
  • 1
    @Hurkyl: Before the first standardization of C++, where the effective standard was the ARM (the Annotated Reference Manual by Stroustrup and Ellis), deleting via a pointer to `const` object was invalid. This was changed in C++98, the first standard, because it was impractical. Implicit destructor call on a constant object has always been valid and guaranteed, AFAIK. I am not sure about explicit destructor call on constant object. Sorry. – Cheers and hth. - Alf Sep 19 '15 at 14:40
  • @Hurkyl For all I know, nothing in the Standard says that evaluating a pseudo-destructor-call with a constant expression as its LHS has UB. Additionally, there's [class.dtor]p2 *"A destructor can be invoked for a `const`, `volatile` or `const volatile` object. `const` and `volatile` semantics are not applied on an object under destruction. They stop being in effect when the destructor for the most derived object starts."* which does not specifically talk about *implicit invocation*, but generally about invocation of a dtor (but that's irrelevant here, since OP doesn't invoke a dtor). – dyp Sep 19 '15 at 14:46
  • +1 for this answer as well for the question because I learnt something new. One might also point to 7.1.6.2 [dcl.type.simple] for an explanation as to why it only works with the `typedef`, namely the fact that a *type-name* in the context of the standard can be a *typedef-name* but not an `int` type specifier. – Christian Hackl Sep 19 '15 at 16:18
  • I don't get the bit about the function call operator. – Lightness Races in Orbit Sep 19 '15 at 20:06
  • @LightnessRacesinOrbit: That's just the `()` parenthesis. I.e., *expr* `.INT::~INT` represents the destructor, and the only thing you can do with it is add a pair of parentheses `()`, a function call. – Cheers and hth. - Alf Sep 19 '15 at 20:41
  • I've never heard of the function name being deemed an _operand_ of the function call operator, but okay. – Lightness Races in Orbit Sep 19 '15 at 20:47