{"id":24518233,"url":"https://github.com/pyzh/borrow","last_synced_at":"2025-03-15T11:12:11.211Z","repository":{"id":196577992,"uuid":"164427339","full_name":"pyzh/borrow","owner":"pyzh","description":null,"archived":false,"fork":false,"pushed_at":"2017-01-03T07:48:27.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-22T01:41:26.778Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pyzh.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2019-01-07T12:09:41.000Z","updated_at":"2019-01-07T12:09:44.000Z","dependencies_parsed_at":null,"dependency_job_id":"52fabc6d-d067-4e44-bed8-4e4cde4c7dfa","html_url":"https://github.com/pyzh/borrow","commit_stats":null,"previous_names":["pyzh/borrow"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyzh%2Fborrow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyzh%2Fborrow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyzh%2Fborrow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyzh%2Fborrow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pyzh","download_url":"https://codeload.github.com/pyzh/borrow/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243719398,"owners_count":20336607,"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":[],"created_at":"2025-01-22T01:41:09.820Z","updated_at":"2025-03-15T11:12:11.193Z","avatar_url":"https://github.com/pyzh.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"======\nborrow\n======\n\nConstant expressions in C++ are not as constant as you might think. For\nexample, the following program will cause a compiler error.\n\n.. code:: c++\n\n    // example0.cpp\n    #include \"borrow.hpp\"\n\n    int\n    main() {\n      using borrow::ended;\n\n      struct $ {\n        struct Lifetime;\n      };\n\n      static_assert(not ended(${}, 0));\n\n      struct $::Lifetime {\n        struct Ended;\n      };\n\n      static_assert(not ended(${}, 0)); // compiler error here\n    }\n\nThis feature does not seem to be useful at first glance. Thanks to the\nbrilliant `friend injection techique discovered by Filip Roséen`__ , stateful\nmetaprogramming has become feasible in C++.\n\n.. __: http://b.atch.se/posts/non-constant-constant-expressions/\n\nRust, a promising rival to C++, is famed for its borrow checker. With stateful\nmetaprogramming, we can have the same mechanism implemented at compile-time in\nC++.\n\n.. code:: c++\n\n    // example1.cpp\n    #include \u003cnew\u003e\n    #include \u003ctype_traits\u003e\n    #include \u003cutility\u003e\n    #include \"borrow.hpp\"\n\n    template\u003ctypename T\u003e\n    struct Container {\n      char buf[sizeof(T)];\n\n      void\n      write(T\u0026\u0026 item) {\n        T *ptr = static_cast\u003cT *\u003e(static_cast\u003cvoid *\u003e(buf));\n        new (ptr) T(::std::move(item));\n      }\n\n      T\n      read() {\n        T *ptr = static_cast\u003cT *\u003e(static_cast\u003cvoid *\u003e(buf));\n        T item = ::std::move(*ptr);\n        ptr-\u003e~T();\n        return item;\n      }\n\n      T\u0026\n      get() {\n        T *ptr = static_cast\u003cT *\u003e(static_cast\u003cvoid *\u003e(buf));\n        return *ptr;\n      }\n\n      T const\u0026\n      get() const {\n        T *ptr = static_cast\u003cT *\u003e(static_cast\u003cvoid *\u003e(buf));\n        return *ptr;\n      }\n    };\n\n    template\u003ctypename T, typename $,\n             bool A = ::borrow::available(${}),\n             typename = typename ::std::enable_if\u003cA\u003e::type\u003e\n    auto\n    get(::borrow::BorrowPtr\u003cContainer\u003cT\u003e,$\u003e ptr) {\n      return ::borrow::BorrowPtr\u003cT,$\u003e { (*ptr).get() };\n    }\n\n\n    int\n    main() {\n      auto c = Container\u003cint\u003e {};\n      c.write(0);\n\n      BEGIN_LIFETIME($);\n      auto p = ::borrow::borrow_mut\u003c$\u003e(c);\n\n      BEGIN_LIFETIME($1);\n      auto p1 = ::borrow::borrow_mut\u003c$1\u003e(p);\n\n      auto p2 = get(p1);\n\n      *p2 = 1;\n\n      END_LIFETIME($1);\n\n      *p2; // compiler error here\n\n      END_LIFETIME($);\n    }\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpyzh%2Fborrow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpyzh%2Fborrow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpyzh%2Fborrow/lists"}