{"id":2089,"date":"2024-09-18T08:21:11","date_gmt":"2024-09-18T08:21:11","guid":{"rendered":"https:\/\/cppdepend.com\/blog\/?p=2089"},"modified":"2024-09-18T08:21:17","modified_gmt":"2024-09-18T08:21:17","slug":"c-generic-programming-from-libraries-to-core-projects","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/c-generic-programming-from-libraries-to-core-projects\/","title":{"rendered":"C++ Generic Programming: From Libraries to Core Projects"},"content":{"rendered":"\n<p>C++ generic programming, initially popularized for creating reusable libraries like the Standard Template Library (STL), has recently seen widespread adoption in many C++ projects. The power of <strong>templates<\/strong> allows developers to write code that is flexible, reusable, and type-safe, reducing redundancy while ensuring performance. With modern C++ standards (C++11, C++17, and C++20), template programming has evolved, featuring advanced concepts like <strong>variadic templates<\/strong>, <strong>constexpr<\/strong>, and <strong>SFINAE<\/strong>, which help manage complexity. This trend has made generic programming an integral part of contemporary C++ development.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p><strong>Shifting from OOP to generics in core projects: Runtime Polymorphism to Static Polymorphism<\/strong><\/p>\n\n\n\n<p>Runtime polymorphism plays a crucial role in many <strong>OOP design patterns<\/strong> by allowing objects to be treated uniformly through a common interface or base class. Here&#8217;s how it is used in popular design patterns:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Strategy Pattern<\/strong>: Encapsulates algorithms in classes, allowing them to be swapped at runtime through polymorphism.<\/li>\n\n\n\n<li><strong>Observer Pattern<\/strong>: Allows multiple observers to react to changes in a subject, with dynamic binding of observer methods.<\/li>\n\n\n\n<li><strong>Factory Method Pattern<\/strong>: Returns different types of objects, which share a common interface, decided at runtime.<\/li>\n<\/ol>\n\n\n\n<p>Runtime polymorphism, is implemented using <strong>inheritance<\/strong> and <strong>virtual functions<\/strong>. Here, the method to be invoked is determined at runtime using a vtable. This approach adds flexibility because the exact type doesn\u2019t need to be known until runtime, but it introduces some overhead due to virtual function lookups.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">class Base {\npublic:\n    virtual void doSomething() = 0;\n};\n\nclass Derived : public Base {\npublic:\n    void doSomething() override {\n        \/\/ Implementation\n    }\n};<\/pre><\/div>\n\n\n\n<p>Static polymorphism, on the other hand, is achieved through <strong>templates<\/strong> or <strong>function overloading<\/strong>, where the method to be invoked is determined at compile time. This provides better performance since there&#8217;s no overhead of function lookup at runtime. A common example is <strong>CRTP (Curiously Recurring Template Pattern)<\/strong>, which uses templates to achieve polymorphism without virtual functions.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">template&lt;typename T&gt;\nclass Base {\n    void doSomething() {\n        static_cast&lt;T*&gt;(this)-&gt;doSomething();\n    }\n};<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Key Differences:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Performance<\/strong>: Static polymorphism has better performance due to compile-time resolution, while runtime polymorphism incurs a small overhead for dynamic dispatch.<\/li>\n\n\n\n<li><strong>Flexibility<\/strong>: Runtime polymorphism is more flexible, allowing changes in behavior based on the actual object type at runtime, whereas static polymorphism requires the type to be known at compile time.<\/li>\n\n\n\n<li><strong>Use Case<\/strong>: Static polymorphism is ideal for scenarios where performance is critical, and runtime polymorphism is useful when flexibility and extensibility are important.<\/li>\n<\/ul>\n\n\n\n<p>In core projects, dynamic polymorphism has been largely replaced by static polymorphism, with many template-based idioms gaining popularity. This shift has significantly changed the way C++ developers write code, offering better performance and compile-time optimizations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++ generic programming, initially popularized for creating reusable libraries like the Standard Template Library (STL), has recently seen widespread adoption in many C++ projects. The power of templates allows developers to write code that is flexible, reusable, and type-safe, reducing redundancy while ensuring performance. With modern C++ standards (C++11, C++17, and C++20), template programming has &hellip; <a href=\"https:\/\/cppdepend.com\/blog\/c-generic-programming-from-libraries-to-core-projects\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;C++ Generic Programming: From Libraries to Core Projects&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[67,46],"class_list":["post-2089","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c-2","tag-oop"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/2089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=2089"}],"version-history":[{"count":5,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/2089\/revisions"}],"predecessor-version":[{"id":2094,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/2089\/revisions\/2094"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=2089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=2089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=2089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}