{"id":15047408,"url":"https://github.com/bugdea1er/defer","last_synced_at":"2026-03-16T16:37:09.083Z","repository":{"id":217222102,"uuid":"743286311","full_name":"bugdea1er/defer","owner":"bugdea1er","description":"`defer` keyword - A Swift-like `defer` keyword for C++11 and later in a single-file header-only library","archived":false,"fork":false,"pushed_at":"2024-01-21T20:24:12.000Z","size":8,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-20T15:59:20.030Z","etag":null,"topics":["cpp11","defer","swift"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bugdea1er.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2024-01-14T21:51:15.000Z","updated_at":"2024-02-07T09:16:26.000Z","dependencies_parsed_at":"2024-01-21T15:48:30.448Z","dependency_job_id":null,"html_url":"https://github.com/bugdea1er/defer","commit_stats":null,"previous_names":["bugdea1er/defer"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bugdea1er%2Fdefer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bugdea1er%2Fdefer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bugdea1er%2Fdefer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bugdea1er%2Fdefer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bugdea1er","download_url":"https://codeload.github.com/bugdea1er/defer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243475328,"owners_count":20296714,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp11","defer","swift"],"created_at":"2024-09-24T20:57:54.797Z","updated_at":"2025-12-29T16:55:05.206Z","avatar_url":"https://github.com/bugdea1er.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"## `defer` keyword for C++11 from Swift\n\nThe defer statement defers execution until the current scope is exited, and consists of the defer keyword and the statements to be executed later.\n\n\n\u003e [!NOTE]\n\u003e [RAII](https://en.cppreference.com/w/cpp/language/raii) approach is still recommended, especially when you want to transfer the ownership of the aquired objects. Checkout [C++ Code Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#e6-use-raii-to-prevent-leaks) on this recommendation.\n\u003e Use this defer syntax only when working with older (e.g. C) APIs and without transferring ownership.\n\n## Introduction\n\nThe defer statement is used to execute a set of statements before the code execution leaves the current block of code. This is useful for performing necessary cleanup tasks, regardless of how the execution leaves the block of code. For instance, it can be used to ensure that file descriptors are closed and memory is freed.\n\n\u003e [!CAUTION]\n\u003e The defer statement cannot contain any code that would transfer control out of the statements, such as a break or return statement, or by throwing an error. If the error is thrown anyway, std::terminate is called.\n\n```cpp\n#include \u003cdefer/defer.hpp\u003e\n\nauto readfile() {\n  FILE* file = fopen(\"file\", \"rb\");\n  defer {\n    fclose(file);\n  };\n\n  // work with file without worrying about closing it\n  // fclose(file) is called when leaving this function in any way\n}\n```\n\nThe above example uses a defer statement to ensure that the file is always\ncloses when returning or throwing from this function.\n\n## Integration\n\n### Copy a single file\n\nSince this is a header-only library, you can just copy the [defer.hpp](https://github.com/bugdea1er/defer/blob/main/include/defer/defer.hpp) file to your project and enable C++11 \n\n### CMake integration\n\nYou can also use the CMake interface target `defer::defer` and described below:\n\n#### Embedded\n\nTo embed the library into your existing CMake project, place the entire source tree in a subdirectory (for example, using `git submodule` commands) and call `add_subdirectory()` in your `CMakeLists.txt` file:\n```cmake\nadd_subdirectory(defer)\n...\nadd_library(foo ...)\ntarget_link_libraries(foo PRIVATE defer::defer)\n```\n\n#### FetchContent\n\nYou can also use the FetchContent functions to automatically download a dependency. Put this in your `CMakeLists.txt` file:\n```cmake\ninclude(FetchContent)\n\nFetchContent_Declare(defer URL https://github.com/bugdea1er/defer/releases/download/v1.0/defer.tar.xz)\nFetchContent_MakeAvailable(defer)\n\ntarget_link_libraries(foo PRIVATE defer::defer)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbugdea1er%2Fdefer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbugdea1er%2Fdefer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbugdea1er%2Fdefer/lists"}