<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Neville Sinnamon on Medium]]></title>
        <description><![CDATA[Stories by Neville Sinnamon on Medium]]></description>
        <link>https://medium.com/@neville.sinnamon?source=rss-7e2f77bc669d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6ybWu119PhcXFyxvaBdvdw.png</url>
            <title>Stories by Neville Sinnamon on Medium</title>
            <link>https://medium.com/@neville.sinnamon?source=rss-7e2f77bc669d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 16:33:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@neville.sinnamon/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[C++20 Concepts a simple example]]></title>
            <link>https://medium.com/version-1/c-20-concepts-a-simple-example-604bac0f6bb1?source=rss-7e2f77bc669d------2</link>
            <guid isPermaLink="false">https://medium.com/p/604bac0f6bb1</guid>
            <category><![CDATA[cpp20]]></category>
            <category><![CDATA[cplusplus]]></category>
            <category><![CDATA[concept]]></category>
            <category><![CDATA[c-plus-plus-language]]></category>
            <category><![CDATA[template]]></category>
            <dc:creator><![CDATA[Neville Sinnamon]]></dc:creator>
            <pubDate>Fri, 31 Oct 2025 09:29:24 GMT</pubDate>
            <atom:updated>2025-10-31T09:29:24.764Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*YqEQ500JlOBksJXdDh1BeA.png" /><figcaption>C++20 Concepts</figcaption></figure><p>This blog is aimed at experienced C++ developers who are familiar with templates and would be interested in learning about the C++ feature “<strong>concept</strong>” introduced in C++20.</p><p>If you are not familiar with “<strong>concepts</strong>”, please see <a href="https://github.com/AnthonyCalandra/modern-cpp-features#concepts">GitHub — AnthonyCalandra/modern-cpp-features: A cheatsheet of modern C++ language and library features</a> for a quick summary.</p><p>Concepts allow conditional templates to be written simpler and more intuitively than C++11 and much simpler than C++98.</p><p>This blog contains a simple example to show how C++20 concepts can be used. Then compare it with, two C++11 solutions using <strong>decltype</strong>, which although neat, are not as intuitive. And finally, the complicated way it had to be done in C++98.</p><h3>Example Background</h3><p>Conditional templates are useful in the field of protocols. One example protocol is sending messages to, and receiving messages from, an exchange. Typical messages used in this protocol are: A “<strong>New Order Single</strong>” to send an order to the exchange; the Exchange can respond with an “<strong>Ack</strong>” or “<strong>Reject</strong>”; if accepted the exchange can reply with a “<strong>Fill</strong>” when the order is executed; or possibly a “<strong>PartialFill</strong>” if part of the order is executed. Other possible messages are “<strong>Cancel</strong>” and “<strong>Update/Replace</strong>”.</p><p>For each message we want to be able to extract the id. Sometimes the id may be in a field called “<strong>id</strong>”, but in other messages such as “<strong>replace</strong>”, the id could be stored in a field called “<strong>parent_id</strong>”, and in yet other messages such as “<strong>heartbeat</strong>” there is no id. So, what is the best way to create a consistent interface to extract the id from the message?</p><h3><strong>Possible OO Solution</strong></h3><p>Some developers may define an abstract class (interface) to get and set the id, then for each message create a new sub class to implement the interface. Initially, this seems like a good solution. However, even with only seven messages there would be duplication because there will be at most three ways to extract the id, and probably only two. This solution creates a lot of new classes, and worse if there happened to be twenty messages, we would need twenty new implementations of the interface, and most would use the same implementation. See <a href="https://github.com/NevilleSinnamon/cppextra/blob/main/concept/sub.cpp">cppextra/concept/sub.cpp at main · NevilleSinnamon/cppextra</a> for an example.</p><h3>C++20 example using concepts</h3><p>So how do we implement this using templates and concepts?</p><p>Firstly, in the interests of simplicity and brevity, the number of messages will be reduced to three. The three messages will be <strong>NOS</strong> (containing an id), <strong>Replace</strong> (containing a parent_id) and <strong>HB</strong> (with no id). See code snippet below. Note: the code for this is stored in github, and can be found here <a href="https://github.com/NevilleSinnamon/cppextra/blob/main/concept/concept.cpp">cppextra/concept/concept.cpp at main · NevilleSinnamon/cppextra</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/467/1*k2XJYHFwQhRPPJoVtjSZyA.png" /></figure><h3>Concepts</h3><p>Two concepts are defined: the first “<strong>has_id</strong>” is true if the class contains a data member called “id”; the second “<strong>has_parent_id</strong>” is true if the class contains a data member called “parent_id” (see below). These new concepts will be used to create the “which_id” template functions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/1*I38jUrKhV7PQqPEFvKWbmg.png" /></figure><h3>Which_id</h3><p>Below are three template functions all called “<strong>which_id</strong>”. Note: on lines 22 and 28 concepts <strong>has_id</strong> and <strong>has_parent_id</strong> are used rather than the more general <em>typename </em>shown on line 16.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/813/1*QYxQ2TaCrywULD9sume1AA.png" /></figure><p>If which_id is passed a message of class NOS (having an “id” data member, satisfying the concept “has_id”) then the function defined on line 22 will be called.</p><p>Similarly, when a message of class Replace is passed to which_id, the function defined on line 28 is called because it has a “parent_id” data member satisfying the concept “has_parent_id”.</p><p>And finally, the default which_id defined on line 16 is called when a message has neither an “id” nor “parent_id”, as it satisfies neither concept (e.g. a message of class HB).</p><h3><strong>IdHandler</strong></h3><p>Next, three template classes called IdHandlerType are defined to set and get the id from the class. The three template classes have two template parameters: the first corresponds to the message (e.g. NOS, Replace, or HB); the second parameter indicates how to access the id, using IdType (see below).</p><p>The template class defined on line 47 accesses the id using the data member “id” (lines 50 and 51). The template defined on line 54 accesses the id using the data member “parent_id” (lines 57 and 58). A third benign template is defined on line 40 when there is no id.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*gKdukYLqa_LdOlN1DFQdCQ.png" /></figure><p>The real magic is on line 62, which uses a template function “which_id” (described above) to decide which template to use, meaning the user can use the template “IdHandler” to access the id in a consistent manner, no matter what the message is, or how the id is stored.</p><h3>How IdHandler is used</h3><p>Three messages are created (nos, hb, and rep) and each is passed to the method handle_id, which extracts the id, using the IdHandler, from each message in a consistent way. There is no need to check which class the message is, or what data member the id is stored in (if stored at all) that is all taken care of by the IdHandler.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/467/1*YMe7OP4un6QgLX3WNoQ3-w.png" /></figure><h3>C++11 Example using decltype</h3><p>This example will use the same data types as the C++20 code along with the same main method, and IdHandlerTypes, even IdType is the same. The only difference is the “which_id” function which is shown below. See <a href="https://github.com/NevilleSinnamon/cppextra/blob/main/concept/decltype.cpp">cppextra/concept/decltype.cpp at main · NevilleSinnamon/cppextra</a> for the entire source file.</p><p>This solution uses <strong>decltype </strong>and a C++ technique known as SFINAE (Substitution Failure is not an Error) to create the which_id function required for each message class. For example, on line 11 the template function which_id will be generated if the message class has a data member called id. Similarly, on line 17 the method will be generated if the message has a data member called parent_id.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*Dp2CjokS_MA2VonXk0XlgQ.png" /></figure><p>Finally, the method on line 8 (defined with an ellipse) is used when the message has neither an “id” nor “parent_id” data member. This is because the method has the lowest priority and is only called if the two templates (lines 11 and 17) fail to be generated.</p><h3>C++ 11 using decltype to create IdHandler without which_id</h3><p>This solution also uses decltype and SFINAE. It starts with general template class, and two specialised template classes for the IdHandler. Using decltype this way is clever and neat, but may not be the most intuitive. For reference, the code can be found in github, along with two examples of template function and class specialization.</p><p><a href="https://github.com/NevilleSinnamon/cppextra/blob/main/concept/no_which.cpp">cppextra/concept/no_which.cpp at main · NevilleSinnamon/cppextra</a></p><p><a href="https://github.com/NevilleSinnamon/cppextra/blob/main/concept/eq.cpp">cppextra/concept/eq.cpp at main · NevilleSinnamon/cppextra</a></p><p><a href="https://github.com/NevilleSinnamon/cppextra/blob/main/concept/couple.cpp">cppextra/concept/couple.cpp at main · NevilleSinnamon/cppextra</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/925/1*CXclWKnrQUb0rfVso7p1nw.png" /></figure><p>Other C++11 solutions using enable_if and</p><p>There are two other ways to do this in C++11, but rather than explain them in this blog, I’ve provided links to the code. The first two links are a header and a source file to create a has_id&lt;T&gt;::value template to be used instead of which_id&lt;T&gt;. The third link shows how to use <strong>enable_if </strong>to provide a solution.</p><p>cppextra/concept/IdType.h at main · NevilleSinnamon/cppextra</p><p><a href="https://github.com/NevilleSinnamon/cppextra/blob/main/concept/id_type.cpp">cppextra/concept/id_type.cpp at main · NevilleSinnamon/cppextra</a></p><p><a href="https://github.com/NevilleSinnamon/cppextra/blob/main/concept/enable_if.cpp">cppextra/concept/enable_if.cpp at main · NevilleSinnamon/cppextra</a></p><h3>C++98 how to do it the old way</h3><p>Firstly, because C++98 does not have the “<strong>using”</strong> keyword, nor does it allow templates to be used with <strong>typedef</strong>, it meant that the IdHandler had to be defined using a nested <strong>typedef </strong>inside a class (see code snippet below). Note: the whole code can be found here <a href="https://github.com/NevilleSinnamon/cppextra/blob/main/concept/cond98.cpp">cppextra/concept/cond98.cpp at main · NevilleSinnamon/cppextra</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*KFhI1ctp6gLzzBOih9TAfA.png" /></figure><p>Secondly, because we do not have concepts or decltype to specify multiple messages classes therefore each message class that is different from the general template must be specified by its own specialised template. The downside to this if there are many messages this would produce a lot of duplication. Of course, the general template should be used for the most common case, which would reduce some of the duplication.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/889/1*CkzYTJuiq5AlcsWjARmnxQ.png" /></figure><h3><strong>Conclusion</strong></h3><p>Although C++98 is <strong>not </strong>the best option due to the lack of features (such as: “<strong>using</strong>”, “<strong>decltype</strong>”, and “<strong>concept</strong>”), it is better than inheriting an abstract class and creating a new subclass for each message.</p><p>At the very least a developer should be using C++11, however, concepts in C++20 are by far the simplest and most readable.</p><h4>About the author</h4><p>Neville Sinnamon is a Senior Software Engineer here at <a href="https://www.version1.com/">Version 1</a>.</p><h3><strong>Appendix A</strong>: building on linux</h3><p>This code was built and run on ubuntu 20, running on WSL on a windows laptop. You may also need to install a version of g++ that can run C++20 (for concepts).</p><p><em>sudo apt update &amp;&amp; sudo apt install build-essential</em></p><p><em>sudo apt-get install g++-10</em></p><p>download the example code as follows.</p><p><em>git clone </em><a href="/service/mailto:git@github.com"><em>git@github.com</em></a><em>:NevilleSinnamon/cppextra.git</em></p><p>or <em>git clone </em><a href="https://github.com/NevilleSinnamon/cppextra.git"><em>https://github.com/NevilleSinnamon/cppextra.git</em></a></p><p><em>cd cppextra/concept</em></p><p>build and run using.</p><p><em>make all</em></p><p><em>bin/concept</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=604bac0f6bb1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/version-1/c-20-concepts-a-simple-example-604bac0f6bb1">C++20 Concepts a simple example</a> was originally published in <a href="https://medium.com/version-1">Version 1</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>