c++ - Is there a way to make instantiation with unique_ptr less verbose? -


I have a class with two std :: unique_ptr members. I want to set those members through Constructor.

I am currently doing this:

  std :: unique_ptr < Assignment & gt; Assignment {new assignment {std :: unique_ptr & lt; Variable & gt; {New Variable {"A"}}, std :: unique_ptr & lt; Expression & gt; {New variable {"b"}}}}; With regular pointers, I could:  
  auto assignment = new assignment (new variable {"a"}, new variable {"b"}}; delete assignment;  

Can I make Smart Pointer version somewhat less verbose? I was hoping that something like this might be done.

  std :: unique_ptr & Lt; assignment & gt; assignment {new variable {"a"}, {new variable {"b"}};  

However, the creator of unique_ptr Are, therefore it is not

Is a right forwarding constructor an option?

  // If you are not using mechanical, then you are doing it wrong.Template & lt; typename T, typename ... Args> Inline Stud :: Exclusive_Pit  Make_unique (args & Amp; ... args) {return std :: unique_ptr  {NewT (Standard :: Forwarded & lt; Ergus & gt; (Args) ...}}}} Struct variable {std :: string name_; variable (std :: string name): name_ {std :: move (name)} {}}; Struct assignment {std :: unique_ptr & lt; Variables & gt; A_, b_; Template & lt; Typename t, typename u & gt; Assignment (T & amp; A, U & amp; B): A_ {Make-Intex & Lt; Variable & gt; (STD :: Forward & lt; T & gt; (A))}, B_ {Mikunik & lt; Variable & gt; (Std :: forward & lt; U & gt; b))} {}}; Auto assignment = Macionic & lt; Assignment & gt; ("A", "b");  

Syntax makes a bit simpler, IMO


Comments