@@ -702,8 +702,8 @@ class final_action_success
702
702
}
703
703
}
704
704
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 ))
707
707
{ }
708
708
709
709
final_action_success (final_action_success const &) = delete ;
@@ -760,9 +760,10 @@ auto to_string(...) -> std::string {
760
760
//
761
761
// As part of embracing compatibility while also reducing what we have to
762
762
// 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
765
765
// another that goes hand in hand with that, hence this section...
766
+ // but see caveat note at the end.
766
767
//
767
768
// -----------------------------------------------------------------------
768
769
//
@@ -777,18 +778,34 @@ class c_raii {
777
778
, dtor{ [](void * x) { (D)(x); } }
778
779
{ }
779
780
780
- ~c_raii () { dtor (t); }
781
+ ~c_raii () { if (dtor) dtor (t); }
781
782
782
783
operator T&() { return t; }
783
784
784
- c_raii (c_raii const &) = delete ;
785
+ c_raii (c_raii const &) = delete ;
785
786
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 ; }
786
789
};
787
790
788
791
auto 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 );
790
797
}
791
798
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
+
792
809
793
810
}
794
811
0 commit comments