www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why cant I place a stuct on a funciton stack without blitting it

reply Byron <byron.heads gmail.com> writes:
And why is RAII verbose?

I have a transactional struct:

struct Tans {
 bool committed;

 this(..) {
   committed = false;
   .. set up
 } 

 void commit() {
   .. commit
   committed = true;
 }

 ~this() {
   if(!committed) {
     .. roll back commit
   }
 }
}


I want to use this in a lot of functions

void foo() {
  Trans trans(...); /// wont compile, using Trans as a type...
  auto tans = Tans(...); /// calls destructor on the blit?
}

I was looking around in stdlib, lots of structs that are RAII have 
refcounting.  I have no need for refcounting as my transactions are unique.
also I do not want to write scope(exit) everywhere, making the transaction 
instance should be all I need.

Also the need to blit here seems silly, I just want a simple scoped RAII 
transaction/lock object. 


DMD 2.065 Windows
Apr 05 2014
next sibling parent reply Byron <byron.heads gmail.com> writes:
On Sat, 05 Apr 2014 15:40:13 +0000, Byron wrote:

 And why is RAII verbose?
 
 I have a transactional struct:
 
 struct Tans {
  bool committed;
 
  this(..) {
    committed = false;
    .. set up
  }
 
  void commit() {
    .. commit committed = true;
  }
 
  ~this() {
    if(!committed) {
      .. roll back commit
    }
  }
 }
 
 
 I want to use this in a lot of functions
 
 void foo() {
   Trans trans(...); /// wont compile, using Trans as a type...
   auto tans = Tans(...); /// calls destructor on the blit?
 }
 
 I was looking around in stdlib, lots of structs that are RAII have
 refcounting.  I have no need for refcounting as my transactions are
 unique.
 also I do not want to write scope(exit) everywhere, making the
 transaction instance should be all I need.
 
 Also the need to blit here seems silly, I just want a simple scoped RAII
 transaction/lock object.
 
 
 DMD 2.065 Windows
Actually it looks like an exception was causing the log to run out of order. Only though it why Tans trans(...); would not work
Apr 05 2014
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/05/2014 08:48 AM, Byron wrote:

 Tans trans(...);  would not work
It works in C++. The following is one way of constructing in D: Tans trans = Tans(42); Ali
Apr 05 2014
prev sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Saturday, 5 April 2014 at 15:40:13 UTC, Byron wrote:
   auto tans = Tans(...); /// calls destructor on the blit?
This does NOT call postblit nor destructor. It's an actual declaration syntax.
 auto tans = Tans.init;
Will not call postblit either. Finally:
 auto tans = makeTans(args...);
is guaranteed (by spec) to not call postblit if "makeTans" qualifies for [N]RVO.
Apr 05 2014