decltype

Jonathan Wakely jwakely.gcc@gmail.com
Tue Feb 25 17:17:00 GMT 2014


On 25 February 2014 15:50, Jonathan Wakely wrote:
> This question is not about GCC so would be more appropriate in a
> general C++ forum, e.g. stackoverflow.com
>
>
> On 25 February 2014 15:46, Graziano Servizi wrote:
>> Could you kindly explain to me how and when the decltype keyword do have a
>> tilde character prefixed?
>> I'm unable to figure out the cases in which such a syntax would be used and
>> I found it as an example of an "id-expression" together with the name of a
>> destructor: should be to explicitly call a destructor of some class?
>
> Yes, it's used in a pseudo-destructor call.
>
> For example, to destroy an object returned by a function:
>
> template<typename Func>
> void foo(Func f)
> {
>   auto obj = f();
>   obj.~decltype(obj)();
> }
>
> This is equivalent to:
>
> template<typename Func>
> void foo(Func f)
> {
>   auto obj = f();
>   using Object = decltype(obj);
>   obj.~Object();
> }
>
> This syntax is very rarely needed.

N.B. this syntax is not yet supported by G++.

Also, the functions above have undefined behaviour, because the
destructor for obj will be run twice.  Don't use this form of
destructor call unless you really know what you're doing.



More information about the Gcc-help mailing list