diff --git a/src/nvcore/FileSystem.cpp b/src/nvcore/FileSystem.cpp index aa7a920..8c09c16 100644 --- a/src/nvcore/FileSystem.cpp +++ b/src/nvcore/FileSystem.cpp @@ -81,11 +81,11 @@ bool FileSystem::copyFile(const char * src, const char * dst) { FILE * fsrc = fileOpen(src, "rb"); if (fsrc == NULL) return false; - NV_ON_RETURN(fclose(fsrc)); + defer{ fclose(fsrc); }; FILE * fdst = fileOpen(dst, "wb"); if (fdst == NULL) return false; - NV_ON_RETURN(fclose(fdst)); + defer{ fclose(fdst); }; char buffer[1024]; size_t n; diff --git a/src/nvcore/nvcore.h b/src/nvcore/nvcore.h index 05a062e..329a7ba 100644 --- a/src/nvcore/nvcore.h +++ b/src/nvcore/nvcore.h @@ -326,14 +326,13 @@ namespace nv { F f; }; - template - ScopeExit MakeScopeExit(F f) { - return ScopeExit(f); - }; + struct ExitScopeHelp { + template + ScopeExit operator+(T t) { return t; } + }; } -#define NV_ON_RETURN(code) \ - auto NV_STRING_JOIN2(scope_exit_, __LINE__) = nv::MakeScopeExit([=](){code;}) +#define defer const auto& __attribute__((unused)) NV_STRING_JOIN2(defer__, __LINE__) = nv::ExitScopeHelp() + [&]() // Indicate the compiler that the parameter is not used to suppress compier warnings.