{"id":15047372,"url":"https://github.com/lebaoworks/cpp-tips","last_synced_at":"2026-02-17T23:01:50.788Z","repository":{"id":231919336,"uuid":"783026706","full_name":"lebaoworks/cpp-tips","owner":"lebaoworks","description":"Defer like Go, format like C printf and other stuff...","archived":false,"fork":false,"pushed_at":"2025-03-09T12:45:33.000Z","size":157,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-22T22:38:37.123Z","etag":null,"topics":["cpp","cpp11","defer","formatting","tips","tricks"],"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/lebaoworks.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,"dei":null}},"created_at":"2024-04-06T18:25:38.000Z","updated_at":"2024-07-14T15:09:36.000Z","dependencies_parsed_at":"2024-04-06T20:25:04.532Z","dependency_job_id":"0354ef5e-750b-489e-9143-ca366da10021","html_url":"https://github.com/lebaoworks/cpp-tips","commit_stats":null,"previous_names":["lebaoworks/cpp-tips"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/lebaoworks/cpp-tips","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lebaoworks%2Fcpp-tips","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lebaoworks%2Fcpp-tips/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lebaoworks%2Fcpp-tips/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lebaoworks%2Fcpp-tips/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lebaoworks","download_url":"https://codeload.github.com/lebaoworks/cpp-tips/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lebaoworks%2Fcpp-tips/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29561783,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-17T21:50:49.831Z","status":"ssl_error","status_checked_at":"2026-02-17T21:46:15.313Z","response_time":100,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["cpp","cpp11","defer","formatting","tips","tricks"],"created_at":"2024-09-24T20:57:17.245Z","updated_at":"2026-02-17T23:01:50.772Z","avatar_url":"https://github.com/lebaoworks.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cpp-tips\n\n## Introduction  \u003c!-- omit in toc --\u003e\nThis is the collection of snippets I found on internet or made by me.  \n\n## Contents: \u003c!-- omit in toc --\u003e\n- [Defer function](#defer)\n- [Format like printf](#format-like-printf)\n- [Runtime exception with format](#runtime-exception-with-format)\n\n## Defer\n- Requirement: C++11 or later.\n- Source: [pmttavaras's answer](https://stackoverflow.com/a/42060129) on stackoverflow.\n- Modified to use move-semantic for true zero-overhead by me.\n\nDeferred functions will be called at the end of the scope where they were declared, in last-in-first-out order. \n\nSnippet:\n```cpp\n#include \u003cmemory\u003e\n\nstruct defer_dummy {};\ntemplate\u003cclass F\u003e\nstruct deferer\n{\n    F _f;\n    deferer(F\u0026\u0026 f) noexcept : _f(f) {}\n    ~deferer() { _f(); }\n};\ntemplate\u003cclass F\u003e\ninline deferer\u003cF\u003e operator*(defer_dummy, F\u0026\u0026 f) noexcept { return deferer\u003cF\u003e(std::move(f)); }\n#define DEFER_(LINE) zz_defer##LINE\n#define DEFER(LINE) DEFER_(LINE)\n#define defer auto DEFER(__LINE__) = defer_dummy{} *[\u0026]()\n```\n\nSample:\n```cpp\nint main()\n{\n    defer{ printf(\"4\\n\"); };\n    defer{ printf(\"3\\n\"); };\n\n    {\n        defer{ printf(\"1\\n\"); };\n    }\n    \n    defer{ printf(\"2\\n\"); };\n}\n```\n\n## Format like printf\n- Source: [ifreilicht's answer](https://stackoverflow.com/a/26221725) on stackoverflow.\n- Modified to be more efficient by me.\n\nSnippet:\n```cpp\n#include \u003ccstdio\u003e\n#include \u003cmemory\u003e\n#include \u003cstdexcept\u003e\n#include \u003cstring\u003e\n\nnamespace nstd\n{\n    template\u003ctypename... Args\u003e\n    std::string format(const std::string\u0026 format, const Args\u0026... args)\n    {\n        int size_s = std::snprintf(nullptr, 0, format.c_str(), args...);\n        if (size_s \u003c 0) throw std::runtime_error(\"Error during formatting\");\n        std::string ret(size_s, '\\x00');\n        std::snprintf(\u0026ret[0], size_s + 1, format.c_str(), args...);\n        return ret;\n    }\n}\n```\n\nSample:\n```cpp\nint main()\n{\n    std::cout \u003c\u003c nstd::format(\"hello %s!\\n\", \"lebaoworks\");\n}\n```\n\n## Runtime exception with format\n- Source: Me.\n- Requirement: [format](#format-like-printf)\n\nSnippet:\n```cpp\n#include \u003cstdexcept\u003e\n#include \u003cstring\u003e\n\nnamespace nstd\n{\n    struct runtime_error : public std::runtime_error\n    {\n        template\u003ctypename... Args\u003e\n        runtime_error(const std::string\u0026 format, const Args\u0026... args) :\n            std::runtime_error(nstd::format(format, args...)) {}\n    };\n}\n```\n\nSample:\n```cpp\nint main()\n{\n    try\n    {\n        throw nstd::runtime_error(\"test except by %s\", \"lebaoworks\");\n    }\n    catch (std::exception\u0026 e)\n    {\n        printf(\"Got exception: %s\", e.what());\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flebaoworks%2Fcpp-tips","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flebaoworks%2Fcpp-tips","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flebaoworks%2Fcpp-tips/lists"}