@@ -702,8 +702,8 @@ class final_action_success
702702 }
703703 }
704704
705- final_action_success (final_action_success&& other ) noexcept
706- : f(std::move(other .f)), invoke(std::exchange(other .invoke, false ))
705+ final_action_success (final_action_success&& that ) noexcept
706+ : f(std::move(that .f)), invoke(std::exchange(that .invoke, false ))
707707 { }
708708
709709 final_action_success (final_action_success const &) = delete ;
@@ -760,9 +760,10 @@ auto to_string(...) -> std::string {
760760//
761761// As part of embracing compatibility while also reducing what we have to
762762// teach and learn about C++ (which includes the C standard library), I
763- // want to see if we can improve use of the C standard library from Cpp2
764- // code... UFCS is a big part of that, and then RAII destructors is
763+ // was curious to see if we can improve use of the C standard library
764+ // from Cpp2 code... UFCS is a part of that, and then RAII destructors is
765765// another that goes hand in hand with that, hence this section...
766+ // but see caveat note at the end.
766767//
767768// -----------------------------------------------------------------------
768769//
@@ -777,18 +778,34 @@ class c_raii {
777778 , dtor{ [](void * x) { (D)(x); } }
778779 { }
779780
780- ~c_raii () { dtor (t); }
781+ ~c_raii () { if (dtor) dtor (t); }
781782
782783 operator T&() { return t; }
783784
784- c_raii (c_raii const &) = delete ;
785+ c_raii (c_raii const &) = delete ;
785786 auto operator =(c_raii const &) = delete ;
787+ c_raii (c_raii&& that) : t {std::move (that.t )}, dtor {that.dtor } { that.dtor = nullptr ; }
788+ auto operator =(c_raii&& that) { t = std::move (that.t ); dtor = that.dtor ; that.dtor = nullptr ; }
786789};
787790
788791auto fopen ( const char * filename, const char * mode ) {
789- return c_raii ( std::fopen (filename, mode), &fclose );
792+ auto x = std::fopen (filename, mode);
793+ if (!x) {
794+ throw std::make_error_condition (std::errc::no_such_file_or_directory);
795+ }
796+ return c_raii ( x, &fclose );
790797}
791798
799+ // Caveat: There's little else in the C stdlib that allocates a resource...
800+ //
801+ // malloc is already wrapped like this via std::unique_ptr, which
802+ // typically uses malloc or gets memory from the same pool
803+ // thrd_create std::jthread is better
804+ //
805+ // ... is that it? I don't think it's useful to provide a c_raii just for fopen,
806+ // but perhaps c_raii may be useful for bringing forward third-party C code too,
807+ // with cpp2::fopen as a starting example.
808+
792809
793810}
794811
0 commit comments