On Wednesday, July 17, 2024, Lénárd Szolnoki wrote:

How do I achieve the same thing with P3357, especially with taking care of exception safety?



Sebastian and I are now collaborating on P3357. The first revision will have too many alterations to list, so everyone will just have to read the entire thing from start to finish again.

Firstly, 'factory' is renamed to 'construct_modify'.

I haven't put a huge amount of thought into your example, but here's my first lash at it:

Widget foo(int const i)
{
   struct Exception {};

   try
   {
      return construct_modify<Widget>(i,
        [](Widget &w)
        {
          if ( ! w. some_observer() ) throw Exception();
        });
    }
    catch(Exception const&)
    {
       return Widget(0);
    }
}