{"id":20345776,"url":"https://github.com/izzypt/cpp_module_07","last_synced_at":"2026-05-28T13:05:10.477Z","repository":{"id":211902692,"uuid":"730225730","full_name":"izzypt/CPP_Module_07","owner":"izzypt","description":" This module is designed to help you understand Templates in CPP. ","archived":false,"fork":false,"pushed_at":"2023-12-12T13:49:30.000Z","size":15,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-04T15:48:02.149Z","etag":null,"topics":["cpp","templates"],"latest_commit_sha":null,"homepage":"","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/izzypt.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":"2023-12-11T13:21:52.000Z","updated_at":"2023-12-11T17:44:08.000Z","dependencies_parsed_at":"2023-12-11T14:42:52.115Z","dependency_job_id":"0c38c5c6-cbbc-4217-902e-ce9b3019b57c","html_url":"https://github.com/izzypt/CPP_Module_07","commit_stats":null,"previous_names":["izzypt/cpp_module_07"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/izzypt/CPP_Module_07","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP_Module_07","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP_Module_07/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP_Module_07/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP_Module_07/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/izzypt","download_url":"https://codeload.github.com/izzypt/CPP_Module_07/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izzypt%2FCPP_Module_07/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33609274,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-28T02:00:06.440Z","response_time":99,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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","templates"],"created_at":"2024-11-14T22:09:45.525Z","updated_at":"2026-05-28T13:05:10.460Z","avatar_url":"https://github.com/izzypt.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CPP_Module_07\n\nThis module is designed to help you understand Templates in CPP. \n\n# Table of Content\n\n- [What are templates](#templates)\n- [Templates types](#templates_types)\n- [How templates work](#templates_work)\n\n\n\u003ca id=\"templates\"\u003e\u003c/a\u003e\n# What are templates\n\nA template is a simple yet very powerful tool in C++. \n\nThe simple idea is to \u003cins\u003epass the data type as a parameter\u003c/ins\u003e so that we don’t need to write the same code for different data types. \n\nFor example, a software company may need to ```sort()``` for different data types. Rather than writing and maintaining multiple codes, we can write one ```sort()``` and pass the datatype as a parameter. \n\nC++ adds two new keywords to support templates: ````template```` and ```typename```. The second keyword can always be replaced by the keyword ```class```.\n\n\n\u003ca id=\"templates_types\"\u003e\u003c/a\u003e\n# Templates types\n\n![image](https://github.com/izzypt/CPP_Module_07/assets/73948790/d9671a03-f657-453a-aece-b969569026f5)\n\n### Function template\n\nA function template in c++ is a single function template that works with multiple data types simultaneously, but a standard function works only with one set of data types.\n\nA function template starts with the keyword ```template``` followed by template parameter(s) inside \u003c\u003e which is followed by the function definition.\n\n```cpp\n  template \u003ctypename T\u003e\n  T functionName(T parameter1, T parameter2, ...)\n  {\n    // code\n  }\n```\nIn the above code, T is a template argument that accepts different data types (int, float, etc.), and ```typename``` is a keyword.\n\nWhen an argument of a data type is passed to functionName(), the compiler generates a new version of functionName() for the given data type.\n\n### Calling a Function Template\n\nOnce we've declared and defined a function template, we can call it in other functions or templates (such as the main() function) with the following syntax\n\nfunctionName\u003cdataType\u003e(parameter1, parameter2,...);\n\nFor example, let us consider a template that adds two numbers:\n\n```cpp\n  template \u003ctypename T\u003e\n  T add(T num1, T num2)\n  {\n    return (num1 + num2);\n  }\n```\n\nWe can then call it in the main() function to add int and double numbers.\n\n```cpp\nint main() {\n\n    int result1;\n    double result2;\n    // calling with int parameters\n    result1 = add\u003cint\u003e(2, 3);\n    cout \u003c\u003c result1 \u003c\u003c endl;\n\n    // calling with double parameters\n    result2 = add\u003cdouble\u003e(2.2, 3.3);\n    cout \u003c\u003c result2 \u003c\u003c endl;\n\n    return 0;\n}    \n```\n### Class template\n\nThe class template in c++ is like function templates. They are known as generic templates. They define a family of classes in C++. \n\nSyntax of Class Template:\n```cpp\n    template\u003cclass Ttype\u003e\n    class class_name\n    {\n        //class body;\n    }\n```\n\nA class template starts with the keyword template followed by template parameter(s) inside \u003c\u003e which is followed by the class declaration.\n\n```cpp\n    template \u003cclass T\u003e\n    class className\n    {\n        private:\n          T var;\n          ... .. ...\n        public:\n          T functionName(T arg);\n          ... .. ...\n    };\n```\n\nIn the above declaration, T is the template argument which is a placeholder for the data type used, and class is a keyword.\n\nInside the class body, a member variable var and a member function functionName() are both of type T.\n\n### Creating a Class Template Object\n\nOnce we've declared and defined a class template, we can create its objects in other classes or functions (such as the main() function) with the following syntax\n\n```cpp\nclassName\u003cdataType\u003e classObject;\n```\n\nFor example,\n\n```cpp\nclassName\u003cint\u003e classObject;\nclassName\u003cfloat\u003e classObject;\nclassName\u003cstring\u003e classObject;\n```\n\n\u003ca id=\"templates_work\"\u003e\u003c/a\u003e\n# How templates work\n\nTemplates are expanded at compiler time. This is like macros. \n\nThe difference is, that the compiler does type-checking before template expansion. \n\nThe idea is simple, source code contains only function/class, but compiled code may contain multiple copies of the same function/class. \n\n![image](https://github.com/izzypt/CPP_Module_07/assets/73948790/a8aafc84-fe50-4828-bb81-5d7dfc4bb3cf)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizzypt%2Fcpp_module_07","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fizzypt%2Fcpp_module_07","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizzypt%2Fcpp_module_07/lists"}