{"id":18402016,"url":"https://github.com/vimtor/cpp-delegates","last_synced_at":"2025-04-12T17:53:33.021Z","repository":{"id":104206502,"uuid":"172787927","full_name":"vimtor/cpp-delegates","owner":"vimtor","description":"Approximation of C# Delegates, Funcs and Actions on C++","archived":false,"fork":false,"pushed_at":"2019-02-27T16:21:26.000Z","size":12,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-10T19:42:42.632Z","etag":null,"topics":["cplusplus","cpp","delegate","delegates","events","function-pointers"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"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/vimtor.png","metadata":{"files":{"readme":"README.md","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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-02-26T20:46:35.000Z","updated_at":"2019-12-06T13:03:21.000Z","dependencies_parsed_at":null,"dependency_job_id":"19f27761-6fe6-475c-ba70-b1fb01355424","html_url":"https://github.com/vimtor/cpp-delegates","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vimtor%2Fcpp-delegates","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vimtor%2Fcpp-delegates/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vimtor%2Fcpp-delegates/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vimtor%2Fcpp-delegates/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vimtor","download_url":"https://codeload.github.com/vimtor/cpp-delegates/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248610407,"owners_count":21132920,"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":["cplusplus","cpp","delegate","delegates","events","function-pointers"],"created_at":"2024-11-06T02:40:51.152Z","updated_at":"2025-04-12T17:53:32.983Z","avatar_url":"https://github.com/vimtor.png","language":"C++","readme":"# CppDelegates\nApproximation of [C# Delegates](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/), Funcs and Actions on C++\n\n## How to use it\nEven though the three different classes (Delegate, Func and Action) store functions to use them in the future, they serve different purposes. Let's explain them one by one.\n\n### Action\nHis purpose is to store functions whose return type is always void with 0 parameters.\n``` C++\nvoid foo() { std::cout \u003c\u003c \"Something funny\"; }\n\nAction action;\naction += foo;\n```\nThe operator `+=` adds the functions to later be called using the Invoke method.\n``` C++\naction.Invoke();\n// Prints: Something funny\n```\n\nAll of the three classes support function removal by using '-='\n``` C++\naction -= foo;\naction.Invoke();\n\n// Prints: Nothing.\n```\n\n### Func\nHis purpose is to store functions whose return type is always void with 0 or more parameters.\n``` C++\nvoid print(int x, char a) { std::cout \u003c\u003c x \u003c\u003c a; }\n\nFunc\u003cint, char\u003e func;\nfunc += print;\nfunc += print;\n\nfunc.Invoke(5, 'A');\n// Prints: 5A5A\n```\n\n### Delegate\nHis purpose is to store functions whose return type could be whatever, with 0 or more parameters.\n\n``` C++\nDelegate\u003cint()\u003e delegate;\n\ndelegate += []() { return 1; };\ndelegate += []() { return 2; };\ndelegate += []() { return 3; };\n```\n\nA nice feature of the _Delegate_ is that the Invoke method returns a std::vector storing the return values of the functions.\n``` C++\nfor (const auto \u0026item : delegate.Invoke()) std::cout \u003c\u003c item;\n// Prints: 123\n```\n\nAs with _Funcs_, you can also pass parameters to the Invoke method.\n``` C++\nint square(int x) { return x * x; }\n\nDelegate\u003cint(int)\u003e delegate;\ndelegate += square;\n\nstd::cout \u003c\u003c delegate.Invoke(2)[0];\n// Prints: 4\n```\n\n## Frequent questions\n### Can I add member functions?\nYes! Perhaps is not in the most beautiful way, but still does the trick. You just need to use lambdas like this.\n``` C++\nObject obj;\naction += [\u0026]() { obj.something(); } // You can even instantiate the object inside the lambda.\n```\n\n### How do I pass parameters to the functions?\nAgain lambdas. Don't ask me, they do anything. You could use `std::bind` but [clang-tidy advices against it](https://clang.llvm.org/extra/clang-tidy/checks/modernize-avoid-bind.html).\n``` C++\naction += []() { print(5, 'A'); };\naction += []() { print(6, 'B'); };\n\naction.Invoke();\n// Prints: 5A6B\n```\n\n### Something else you didn't mention?\nThanks for asking. The operator `+=` is overloaded to accept a vector.\n``` C++\ndelegate += { foo, bar };\n```\nAt difference of C#, delegates can be **copied by value**.\n``` C++\nDelegate\u003cint\u003e delegate(copyMePls); // This is possible.\n```\nYou can also reset or clear the delegate with `delegate.Clear()`\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvimtor%2Fcpp-delegates","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvimtor%2Fcpp-delegates","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvimtor%2Fcpp-delegates/lists"}