public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* decltype
@ 2014-02-25 15:46 Graziano Servizi
  2014-02-25 15:50 ` decltype Jonathan Wakely
  0 siblings, 1 reply; 3+ messages in thread
From: Graziano Servizi @ 2014-02-25 15:46 UTC (permalink / raw)
  To: gcc-help

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?
Thanks...

                                  G. Servizi

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: decltype
  2014-02-25 15:46 decltype Graziano Servizi
@ 2014-02-25 15:50 ` Jonathan Wakely
  2014-02-25 17:17   ` decltype Jonathan Wakely
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Wakely @ 2014-02-25 15:50 UTC (permalink / raw)
  To: Graziano Servizi; +Cc: gcc-help

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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: decltype
  2014-02-25 15:50 ` decltype Jonathan Wakely
@ 2014-02-25 17:17   ` Jonathan Wakely
  0 siblings, 0 replies; 3+ messages in thread
From: Jonathan Wakely @ 2014-02-25 17:17 UTC (permalink / raw)
  To: Graziano Servizi; +Cc: gcc-help

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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-02-25 17:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-25 15:46 decltype Graziano Servizi
2014-02-25 15:50 ` decltype Jonathan Wakely
2014-02-25 17:17   ` decltype Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).