<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://awalsh128.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://awalsh128.com/" rel="alternate" type="text/html" /><updated>2025-12-31T03:08:28+00:00</updated><id>https://awalsh128.com/feed.xml</id><title type="html">Andrew Walsh’s Website</title><subtitle>Thoughts on Software Engineering.</subtitle><author><name>Andrew Walsh</name><email>awalsh128@gmail.com</email></author><entry><title type="html">Interviews are the Worst</title><link href="https://awalsh128.com/interviews-are-the-worst/" rel="alternate" type="text/html" title="Interviews are the Worst" /><published>2022-02-14T00:00:00+00:00</published><updated>2022-02-14T07:00:01+00:00</updated><id>https://awalsh128.com/interviews-are-the-worst</id><content type="html" xml:base="https://awalsh128.com/interviews-are-the-worst/"><![CDATA[<p>If you have experience in the software engineering field, an interview process can test the experience you have; projects, production scenarios, designs. Unfortunately, for junior positions and those out of college, there isn’t much to test for. Since most are computer scientists, you could test knowledge of algorithms and data structures but does that really prove the ability of a candidate? Knowing the upper bound complexity of an algorithm’s runtime is important but what about when a constant multiplying factor means the difference between production being up and down. We don’t have a solution for the Traveling Salesman Problem but we have very good and practical approximations (e.g. flight scheduling).</p>

<p>As an engineer, we have to think about what is the best approximation and trade offs among different solutions when posed with a hard problem. While the CS field greatly informs software engineering and provides it with a fundamental basis to extend from, it doesn’t wholly prepare a junior software engineer for working in the field, especially in the parts that matter the most.</p>

<p>So what does this have to do with interviewing software engineers? Well it’s hard because they have little to no experience. That isn’t their fault. It is just the difficult situation that interviewers and interviewees find themselves in. What’s worse is that there are good jobs out there but the candidate ends up having a terrible interviewer.</p>

<h2 id="dynamics">Dynamics</h2>

<p><img src="/assets/img/2022-02-14-interviews-are-the-worst/job-interview-stackoverflow.jpg" alt="Stackoverflow Job Interview" /></p>

<p>As someone that has given many interviews, my worst nightmare is leading the candidate down a road I can’t pull them out of due to a bad question or guidance. For me the worst feeling is holding an interview that wastes the candidate’s time and the opportunity to accomplish their career goals. As an interviewer it is my responsibility that the problem be clear to the candidate and something that reflects actual skills needed.</p>

<h3 id="lack-of-consistent-feedback">Lack of Consistent Feedback</h3>

<p>I have had interviewers go out of the room to take a bathroom break, sit on their phones, or just stare silently while I worked on the problem. An interview is just as much a chance to see how the candidate would work with you as you them. It is also an opportunity to evaluate their teamwork skills. This kind of work is not an island and having a poor team dynamic can ruin projects.</p>

<p>Another problem is when the interviewer lets you hang yourself with your own solution. It is important that the interviewer provide guidance and redirection. Not doing so wastes the entire time left instead of giving a candidate an opportunity to show what they can do after correction. I had an interview with a principal engineer once for their architecture interview (they break them up into categories). In it they asked me to find a way to count the number of unique exceptions in a production system. I went right into showing an architecture of designing a logging system and how the processing would work at scale. They had a line of questioning talking about the logging system itself and then at the end told me I could just hash the exceptions. I was pissed. If the interviewer had told me it would be a simple data structures question I would have answered it right away but was completely misled about the parameters of the interview. It was my fault that I made a simple solution a complex one, but the opportunity was completely missed on seeing what I could do, even if that meant I got marked down.</p>

<h3 id="toy-questions">Toy Questions</h3>

<p>IMO the worst kind of questions are the ones that don’t reflect real work at all. How is reversing a linked list or rotating a matrix going to show that I am a good engineer? Some people say that this just reveals how you think about things and while there is some merit to that, it can be very perilous. It requires the candidate to be well studied in these problem spaces first. A better question reflects not only how they approach something but the problem they are trying to solve. What compounds this problem is when an interviewer has a single toy question they ask all the time and evaluates performance based on their own long time familiarity with the problem. The worst is when the problem requires just one key observation to solving it.</p>

<h3 id="poor-personality">Poor Personality</h3>

<p>I have high anxiety and have had interviews go terribly wrong. This is also why I try really hard to improve myself in the process and be an interviewer that gives the candidate the best opportunity of proving themselves. I have co-workers and friends that I consider to be very high quality engineers that would never had the chance if someone didn’t accommodate them in terms of personality.</p>

<p>So what do I mean by poor personality? The interviewer has the power to make or break in these situations. Imagine some of the difficult people that you had to deal with in your life. Now have them be your interviewer. Dispassionate, judgemental, uncooperative, critical, the stuff of nightmares.</p>

<h3 id="feedback-goes-both-ways">Feedback Goes Both Ways</h3>

<p>If you have an interviewer like this don’t be afraid to call it out afterwards. Some of these kind of interviewers may score you well but they should be getting feedback that this isn’t how to handle it. Be polite but be pointed about the behavior. It may not matter but many companies want this kind of feedback. At the end of the day, any person committed to wanting great candidates cares about the process that gets them to that.</p>

<h2 id="its-not-all-bad">It’s Not All Bad</h2>

<p>For the bad interviews that I have had or heard about, there are plenty of, if not more, good ones too. The interviewers were attentive, guiding and gave problems that were multi layered and faceted. Some of them I came out wanting to be on their team because the collaboration went so well.</p>

<p>The reason I call out the bad interviewers is because candidates tend to put the blame on themselves. There is plenty that can be done to make sure you pass an interview but it is important to know what to look for too. Who wants to work for a company whose values are reflected in their employees this way? The process is as much about you evaluating them as they you and a good interviewer should know this. Although this isn’t a blame game either. Even a bad interviewer could be the gatekeeper to a great job and you will never have to see them again after being hired.</p>

<h2 id="how-should-i-prepare">How Should I Prepare</h2>

<p>The bad interviewer is a caricature. An embodiment of all the things that could go wrong. Really most have a mix of qualities both positive and negative, intentional and not. You will just have to make sure you do well in spite of the circumstances given.</p>

<p><strong>Become a data structures and algorithms gymnast.</strong> I call it this because it is the most rote part. It bites some candidates because they just didn’t do the work. Like a gymnast you need a bunch of practice and be able to contort yourself in different ways because every problem will be some variation.</p>

<h3 id="problem-spaces">Problem Spaces</h3>

<p>What is the problem that is trying to be solved more generally speaking? A problem space doesn’t refer to any specific solution but rather the end goal that needs to be met. If you become familiar with these, you can also identify some of the common components that are used to solve them. Below is an approximate sample of the problem spaces out there.</p>

<h4 id="strings">Strings</h4>

<p><a href="https://www.google.com/search?q=string+interview+questions">Google Search</a> - string interview questions</p>

<p>For example, in bio-informatic key sequencing, the problem space is in looking for specific patterns (e.g. genome sequencing). Most all sequences can be represented using a string abstraction. While the example of key sequencing is a real world example, it can be generalized to ask questions about strings. The generalized problem space would be strings. By strings I don’t mean words or text, I mean a sequence of symbols in the most abstract way.</p>

<p><img src="/assets/img/2022-02-14-interviews-are-the-worst/gene-sequence.png" alt="Gene Sequence" />
<em>Excerpt of a Gene Sequence</em></p>

<p>Given the string problem space, some combinatorial factors, with examples, could be length, affix, repetition, similarity, and cardinality. Combinations of those being:</p>

<ul>
  <li>Least common subsequence.</li>
  <li>Largest common prefix.</li>
  <li>Most unique string.</li>
  <li>Longest repeated string.</li>
  <li>Number of unique characters (i.e. cardinality).</li>
  <li>Words that share the most characters.</li>
</ul>

<p>This is just a small example. Least common subsequence is a popular one but why stop there? If you can do the combinations then you will have mastery. Just remember an interviewer is trying to mix it up too.</p>

<h4 id="encoding">Encoding</h4>

<p><a href="https://www.google.com/search?q=data+compression+interview+questions">Google Search</a> - data compression interview questions</p>

<p><img src="/assets/img/2022-02-14-interviews-are-the-worst/claude-shannon.jpg" alt="Founder of Information Theory" />
<em>Claude Shannon - The OG of Information Theory</em></p>

<p>How would you represent data in memory? Combinatorial factors could be cardinality, word size, and direction. Some combinations:</p>

<ul>
  <li>Number of unique values encountered.</li>
  <li>Counters of letters encountered.</li>
  <li>Compression for streaming data.</li>
  <li>Compression for bounded data.</li>
  <li>Record a sequence of values containing A, B and C.</li>
</ul>

<h4 id="lookup--counting">Lookup &amp; Counting</h4>

<ul>
  <li><a href="https://www.google.com/search?q=hashing+interview+questions">Google Search</a> - hashing interview questions</li>
  <li><a href="https://www.google.com/search?q=computer+counting+interview+questions">Google Search</a> - computer counting interview questions</li>
</ul>

<p>What is the best way to lookup data with the constraints given? Combinatorial factors could be cardinality, length, and match type (whole, partial).</p>

<ul>
  <li>Specific string of unbounded length.</li>
  <li>Strings that share the first 3 characters.</li>
  <li>Specific count of a single character in a corpus.</li>
  <li>Words who share the same 3 letters.</li>
</ul>

<p>Notice how there is some overlap here with strings, that is because lookup frequently is a subset of the string problem space. We could easily apply common solutions from the string problem space to these as well as hashing and graphs (i.e. trie).</p>

<h4 id="graphs">Graphs</h4>

<p><a href="https://www.google.com/search?q=graph+interview+questions">Google Search</a> - graph interview questions</p>

<p><img src="/assets/img/2022-02-14-interviews-are-the-worst/spammers-in-social-graphs.jpg" alt="Spotting Spammers in Social Graphs" />
<em>Spotting Spammers in Social Graphs</em></p>

<p>How do you find specific relationships between entities? Combinatorial factors could be value cardinality, size, edge direction, edge weights and cycles.</p>

<ul>
  <li>Least cost path between two points.</li>
  <li>Detect all cycles.</li>
  <li>All paths to a point from all other points.</li>
  <li>Paths that never leads to a cycle.</li>
  <li>Reverse direction to every odd numbered node and detect all cycles.</li>
  <li>Remove edges in a cycle that would have the most cost.</li>
</ul>

<p>Keep in mind that a linked list and tree are just graphs with constraints. If you learn graphs, you learn trees and linked lists. It’s just with trees their is direction so it can be flipped.</p>

<h4 id="sorting">Sorting</h4>

<p><a href="https://www.google.com/search?q=sorting+interview+questions">Google Search</a> - sorting interview questions</p>

<p>How do you derive meaning (how is it practical?) by ordering some collections of values? Combinatorial factors could be value cardinality, stability, partiality, and comparability.</p>

<ul>
  <li>Group cards by color.</li>
  <li>Sort integers by partial order where some integer k can be 4 positions away from k+1 and k-1.</li>
  <li>Sort a stream of natural numbers.</li>
  <li>Group floats by their whole number and the order they appear (stable).</li>
  <li>Order a deck of cards by rank, color and suit.</li>
</ul>

<h4 id="dynamic-programming">Dynamic Programming</h4>

<p>Honestly I don’t study this and despise its use in interviews. Rather jadedly I would speculate that most interviewers wouldn’t even be able to come up with a DP solution from first principles. Identifying optimal sub-structure really just takes a lot of experience IMO. Much like induction proofs.</p>

<p>I find a better approach is to work a real world problem from several solution approaches and then use the DP method intuitively. This comes after really understanding the problem and consequently its sub-structure when applied in successive steps (either iteratively or recursively).</p>

<h4 id="space--runtime-are-cross-cutting">Space &amp; Runtime Are Cross Cutting</h4>

<p><a href="https://www.google.com/search?q=space+and+time+complexity+analysis+exercises">Google Search</a> - space and time complexity analysis exercises</p>

<p>For all of these problem spaces, it is important to factor in time and space complexity as well. For example, merge sort is best suited for space because the re-ordering happens in blocks. However quick sort is considered the most performant for in-memory on average even though quick sort has the largest worst case. For lookups a bounded set of keys could just use an array (e.g. frequency counting letters).</p>

<p>Bonus points for pointing out engineering ramifications for choosing a solution even if it has the same complexity. It may be just a constant multiplier difference (e.g. 4n and n) but it could mean the difference of thousands of dollars in a production system’s resource usage.</p>

<p>Some tips when analyzing complexity.</p>

<ul>
  <li>How do the variables grow as you take them to infinity? If given <code class="language-plaintext highlighter-rouge">m</code> and <code class="language-plaintext highlighter-rouge">n</code>, does one become insignificant / constant. Sometimes a candidate will give something like <code class="language-plaintext highlighter-rouge">O(m*n)</code> when in reality it is just <code class="language-plaintext highlighter-rouge">O(m)</code>. Another way to look at it below.</li>
  <li>Think of complexity on average but be explicit. Maybe you are dealing with a set of strings. Let’s say the size of the set is <code class="language-plaintext highlighter-rouge">m</code> and the largest length of a string is <code class="language-plaintext highlighter-rouge">n</code>, is the space complexity really <code class="language-plaintext highlighter-rouge">O(m*n)</code>. Possibly in a theoretical sense but realistically <code class="language-plaintext highlighter-rouge">m</code> will be the overwhelming factor; it would be <code class="language-plaintext highlighter-rouge">O(m)</code> on average. Tell the interviewer this though, don’t just give them the answer <code class="language-plaintext highlighter-rouge">O(m)</code></li>
</ul>

<h3 id="think-out-loud">Think Out Loud</h3>

<p>While going through your thought process, make sure you share it with the interviewer like you would in a job. It’s like spreading out your ideas on a table, picking each one up, investigating it, saying what the pros and cons are, and then going with the best candidate at the moment while also being open to improvement. Maybe your idea isn’t the best one but put something out there that you can execute on and refine later. This will give an opportunity for the interviewer to see how you approach problems and judge the qualities of it.</p>

<h3 id="ask-a-lot-of-questions">Ask A Lot Of Questions</h3>

<p>Really ask them. The worst scenario is that you assume too much and go down a bad path. Probing the problem shows that you are thinking about all the possible aspects and applying critical thinking. I have had candidates ask questions that I didn’t even ask myself about the problem (impressive). In my experience, the better candidates were very inquisitive. Be curious and learn more.</p>

<h3 id="think-before-you-type">Think Before You Type</h3>

<p>Maybe you’re nervous, maybe you think you know the answer and you go straight to typing. Stop! Don’t do it. I have been guilty of this myself. Writing code is a matter of self expression for me, a way of speaking. Although I have learned in time that it can also be a way to code yourself into a corner or hit a dead end that requires lots of back tracking and deletion of code. It can also be a bad sign that you are a coder that doesn’t think before you act and can potentially waste large hours of time on bad paths. This is why design is highly valued in software engineering. It forces people to think about the hard problems before they run up against them in code. Same applies to an interview. I have seen candidates rush into a problem only to stop and realize they really didn’t understand it. Don’t do this. Stop and go over your design with the interviewer. Walk through the different approaches, considering each and then selecting the route you want to take. This complements the idea of thinking out loud.</p>

<h3 id="practice-coding--a-lot">Practice Coding .. A LOT</h3>

<p>When walking through mock interviews, always write out your solutions. In this COVID world, that means typing on a keyboard but it doesn’t hurt to do both. Especially if you get called up for an in-person interview.</p>

<h4 id="pick-a-language">Pick a Language</h4>

<p>Don’t pick a language you think they would want you to write, pick one you are good at and know how to use both syntactically and idiomatically. Unfortunately there are companies that want you to code in a specific languages. Personally I think that is a poor choice for the company since they should be looking for talent but sometimes that’s just the way it goes. For me, I would pick Python because it is the easiest to write IMO.</p>

<p>For the language you use, make sure you know:</p>

<ul>
  <li>the syntax of the language,</li>
  <li>the idioms used, and</li>
  <li>the framework functions / classes involved.</li>
</ul>

<p>For example, know how to work with sequences (e.g. lists), manipulate symbols (e.g. casing strings), and things similar. This will come up naturally as you work on solutions. Whatever language you end up picking, always code out the solution in it. It will build up that muscle so you can easily translate your ideas to code quickly.</p>

<h4 id="break-up-the-problem">Break Up the Problem</h4>

<p>Start top down. Meaning start with the top level function and then break up the problem into sub functions. Deal with easy ones first and then slowly work your way towards the hardest. A divide and conquer approach shows you can compartmentalize and break out the problem into sub problems. It is something that is impressive and a lot of candidates don’t do.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">solve_problem</span><span class="p">():</span>
  <span class="n">easy_values</span> <span class="o">=</span> <span class="n">solve_easy_subproblem</span><span class="p">()</span>
  <span class="n">hard_values</span> <span class="o">=</span> <span class="n">solve_hard_subproblem</span><span class="p">()</span>
  <span class="k">return</span> <span class="n">do_trivial_thing</span><span class="p">(</span><span class="n">easy_values</span><span class="p">,</span> <span class="n">hard_values</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">solve_hard_subproblem</span><span class="p">()</span>
  <span class="n">easypartofhard_values</span> <span class="o">=</span> <span class="n">solve_easypartofhard_subproblem</span><span class="p">()</span>
  <span class="n">somewhathard_values</span> <span class="o">=</span> <span class="n">solve_somewhathard_subproblem</span><span class="p">()</span>
  <span class="k">return</span> <span class="n">do_code_timeconsuming_thing</span><span class="p">(</span><span class="n">easypartofhard_values</span><span class="p">,</span> <span class="n">somewhathard_values</span><span class="p">)</span>
</code></pre></div></div>

<p>An additional upside to this is that if you don’t complete the solution in time, the interviewer is able to at least evaluate the parts that you wholly finished. This is especially helpful if you solved the most conceptually hard parts but just didn’t have enough time for the time consuming code part.</p>

<h3 id="manage-your-emotions">Manage Your Emotions</h3>

<p>For some, interviewing is not emotionally taxing but for others it can be debilitating. Personally, this is a big problem for me. I froze up completely for an interview once. I couldn’t even answer a trivial problem. I was so panicked that I couldn’t even think straight. The interviewer was very kind about it all but there was nothing to evaluate, I failed. Some things that helped me:</p>

<ul>
  <li><strong>Practice until it is instinctual.</strong> Do gymnastics until you can cooly answer any interview question or at least get close to it. Interview problems are just combinations; if you learn the fundamentals and apply them in different arrangements, they aren’t so surprising.</li>
  <li><strong>Mock interview a lot.</strong> Have a friend or use an online resource to constantly perform mock interviews. It will help you with anxiety if you have it and keep you accustomed to the interview format. When I have held mock interviews I have intentionally been cold but polite. It wasn’t to be mean but to prepare them. If you have different tiers of jobs you are interested in, interview with the lowest ones first. Less is on the line if you fail and it is the best thing because it is real.</li>
  <li><strong>You can always try again.</strong> Most companies allow a candidate to try again after some time has passed. Just because you failed an interview doesn’t mean you are a bad candidate. It just means you were off or weren’t prepared enough. I’ll be honest, I failed my first interview with Google, and thought I wasn’t good enough. Five years later I tried again and got the job.</li>
  <li><strong>You are good enough.</strong> Imposter syndrome is a big thing. Especially when you work with people that you perceive to be intelligent and/or well accomplished. Sure there are people that have an obvious raw high intelligence but in my experience that is not the majority, and even then, very incongruent. Many intelligent people who I have met are highly driven. Education is a large factor in intelligence. Also intelligence is not some monolith; people express their intelligence through different characteristics as I alluded to with the word incongruent. In other words, intelligence is fluid and if you are driven, you are good enough.</li>
</ul>

<h2 id="resources-to-use">Resources To Use</h2>

<h3 id="competitive-programming">Competitive Programming</h3>

<p>Sites like TopCoder are nice and a way for you to get your chops solving different problems but it is first and foremost a competitive programming site. It is meant to present challenges that are new and novel, eliciting the tersest amount of code in the smallest amount of time. This isn’t to say if you got good at this you wouldn’t do well in an interview, the opposite, but it requires you to inuit the ability to solve based on working through a lot of problems rather than learning the underlying problem spaces.</p>

<h3 id="interview-question-databases">Interview Question Databases</h3>

<p>This pertains to sites like CareerCup where questions are posted that came from actual interviews with tech companies. Keep in mind that a lot of these get posted because they were difficult for the candidate. That means you are more likely to see harder questions and have false expectations of what is required from you. Take these sites with a grain of salt.</p>

<h3 id="online-exercises">Online Exercises</h3>

<p>Online IDEs and sets of exercises that you can drill on are useful (e.g. GeeksForGeeks, HackerRank). I usually pick the data structures and algorithms exercises and just grind on them several times until it becomes second nature. Experiment with different sites and see which one works best for you. The key is that you are able to get through the exercises fairly quickly. A good site should be able to build you up by difficulty so that you can get muscle strength.</p>

<h2 id="conclusion">Conclusion</h2>

<p>That was a lot but I hope it helps. This is rather a brain dump for me of all the thoughts I have had on interviewing in my career. If I were to summarize in just a few points, it would be to:</p>

<ul>
  <li>Drill and drill and drill everyday for several weeks.</li>
  <li>Go through all the data structures and algos.</li>
  <li>Go through all the problem spaces and make up combinations.</li>
  <li>Mock interview and challenge yourself to socially uncomfortable situations.</li>
</ul>

<p>Lastly and most importantly is to know that you can do this. In my experience, lack of preparation has been the biggest hit on candidates not getting a job. Put in the time and it will help a lot in getting there. Good luck!</p>]]></content><author><name>Andrew Walsh</name><email>awalsh128@gmail.com</email></author><category term="Interview" /><summary type="html"><![CDATA[If you have experience in the software engineering field, an interview process can test the experience you have; projects, production scenarios, designs. Unfortunately, for junior positions and those out of college, there isn’t much to test for. Since most are computer scientists, you could test knowledge of algorithms and data structures but does that really prove the ability of a candidate? Knowing the upper bound complexity of an algorithm’s runtime is important but what about when a constant multiplying factor means the difference between production being up and down. We don’t have a solution for the Traveling Salesman Problem but we have very good and practical approximations (e.g. flight scheduling).]]></summary></entry><entry><title type="html">Compiler Basics</title><link href="https://awalsh128.com/compiler-basics/" rel="alternate" type="text/html" title="Compiler Basics" /><published>2021-10-23T00:00:00+00:00</published><updated>2021-10-25T07:00:01+00:00</updated><id>https://awalsh128.com/compiler-basics</id><content type="html" xml:base="https://awalsh128.com/compiler-basics/"><![CDATA[<p>Compilers was one of my favorite classes in school alongside OS and architecture. It gave me a deeper understanding of what happened with my code behind the scenes and insight into how to create my own <a href="https://en.wikipedia.org/wiki/Domain-specific_language">domain specific language</a>. Knowing how compilers work create the link between the abstractions that developers use everyday and the machine executing them below.</p>

<h2 id="phases">Phases</h2>

<p>Compilation comes into five phases.</p>

<ul>
  <li>Scanner - Breaks up text into tokens.</li>
  <li>Parser - Arranges the tokens into relationships that form a tree.</li>
  <li>Symbol Table - Attributes metadata like type and scope to identifiers (e.g. variables, functions, classes).</li>
  <li>Intermediate Representation (IR) - Representation of the code that is typically a series of statements / commands that can be optimized before becoming the assembly code.</li>
  <li>Compiled Code - The actual assembly code that gets generated at the end as a series of <a href="https://en.wikipedia.org/wiki/Opcode">opcodes</a> with their corresponding data and control bits.</li>
</ul>

<p>Of course this description makes a lot of assumptions and doesn’t account for different approaches to compilation but serves well for the purpose of learning and demonstration. For the following phase breakdowns we will use a C like language with a simple set of constructs as a toy use case. Let’s get started…</p>

<h2 id="scanner">Scanner</h2>

<p>This is also known as the lexer or tokenizer. It’s responsibility is to take text and create indivisible units / tokens out of it. For example, the following…</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">123</span><span class="p">;</span>
</code></pre></div></div>

<p>…would be tokenized as <code class="language-plaintext highlighter-rouge">int</code>, <code class="language-plaintext highlighter-rouge">x</code>, <code class="language-plaintext highlighter-rouge">=</code>, <code class="language-plaintext highlighter-rouge">123</code>, and <code class="language-plaintext highlighter-rouge">;</code>.</p>

<p>Let’s take a more complex example.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">foo</span><span class="p">(</span><span class="kt">int</span><span class="o">&amp;</span> <span class="n">bar</span><span class="p">)</span> <span class="p">{</span>
  <span class="n">bar</span> <span class="o">=</span> <span class="mi">123</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>

<p>This would be tokenized as <code class="language-plaintext highlighter-rouge">int</code>, <code class="language-plaintext highlighter-rouge">foo</code>, <code class="language-plaintext highlighter-rouge">(</code>, <code class="language-plaintext highlighter-rouge">int</code>, <code class="language-plaintext highlighter-rouge">&amp;</code>, <code class="language-plaintext highlighter-rouge">bar</code>, <code class="language-plaintext highlighter-rouge">)</code>, <code class="language-plaintext highlighter-rouge">{</code>, <code class="language-plaintext highlighter-rouge">bar</code>, <code class="language-plaintext highlighter-rouge">=</code>, <code class="language-plaintext highlighter-rouge">123</code>, <code class="language-plaintext highlighter-rouge">;</code>, and <code class="language-plaintext highlighter-rouge">}</code>.</p>

<p>As a contrast, let’s look at a Python like snippet…</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">bar</span><span class="p">):</span>
  <span class="n">bar</span> <span class="o">=</span> <span class="mi">123</span>
</code></pre></div></div>

<p>Since these languages treat spaces as part of the language (unlike C-like ones), this would be tokenized as <code class="language-plaintext highlighter-rouge">def</code>, <code class="language-plaintext highlighter-rouge">foo</code>, <code class="language-plaintext highlighter-rouge">(</code>, <code class="language-plaintext highlighter-rouge">bar</code>, <code class="language-plaintext highlighter-rouge">)</code>, <code class="language-plaintext highlighter-rouge">:</code>, <code class="language-plaintext highlighter-rouge">\n</code>, <code class="language-plaintext highlighter-rouge">\s</code>, <code class="language-plaintext highlighter-rouge">\s</code>, <code class="language-plaintext highlighter-rouge">bar</code>, <code class="language-plaintext highlighter-rouge">=</code>, and <code class="language-plaintext highlighter-rouge">123</code>. This approach is a bit nuanced though as any spaces after <code class="language-plaintext highlighter-rouge">:</code> and before newline are not tokenized since they have no meaning for the compiled language (same goes for extraneous newlines). This is why the last C-like example could be expressed as.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">foo</span><span class="p">(</span><span class="kt">int</span><span class="o">&amp;</span><span class="n">bar</span><span class="p">){</span><span class="n">bar</span><span class="o">=</span><span class="mi">123</span><span class="p">;}</span>
</code></pre></div></div>

<p>The only case where spaces have any meaning is to separate identifiers.</p>

<p>Now let’s take an example we will use going forward for all the phases.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span>
  <span class="n">y</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
  <span class="n">y</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>

<h2 id="parser">Parser</h2>

<p>The parser phase transforms the tokens into relationships between them (semantical) where it takes the flat list of tokens and puts them into a tree structure (i.e. <a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree">Abstract Syntax Tree (AST)</a>). Here is what our example looks like.</p>

<p align="center">
  <img src="/assets/img/2021-10-23-compiler-basics/parser.png" width="400" />
</p>

<p>Something useful to know is the difference between the scanner and parser phase.</p>

<ul>
  <li>The scanner phase is the lexical component, it is concerned with the structure of symbols / tokens (in this case a combination of letters, numbers and punctuation).</li>
  <li>The parser phase is the semantic component, it is concerned with the meaning of symbols as they relate in the context of one another. For example…</li>
</ul>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">foo</span><span class="p">()</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="p">}</span>
</code></pre></div></div>

<p>The parenthesis are the symbols present in the function declaration and the function body but they have completely different meaning in their relationship with the rest of the symbols.</p>

<ul>
  <li>One is in the context of a function declaration <code class="language-plaintext highlighter-rouge">foo</code> and denotes the start and end of declared parameters.</li>
  <li>The other imposes order of operation on the expression assigned to the variable <code class="language-plaintext highlighter-rouge">x</code>.</li>
</ul>

<p>This is what makes the parsing / semantic phase different than the scanner / lexical phase.</p>

<h2 id="symbol-table">Symbol Table</h2>

<p>The <a href="https://en.wikipedia.org/wiki/Symbol_table">symbol table</a> provides information about the identifiers such as type and scope. Let’s take the canonical example we have been using and encapsulate it into a function so we can give it some scope.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">foo</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span>
  <span class="kt">int</span> <span class="n">y</span><span class="p">;</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">y</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
  <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
    <span class="n">y</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>

<p>The code gets stored in the symbol table as…</p>

<table>
  <thead>
    <tr>
      <th>Symbol</th>
      <th>Type</th>
      <th>Scope</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>foo</td>
      <td>function, void</td>
      <td>global</td>
    </tr>
    <tr>
      <td>x</td>
      <td>int</td>
      <td>function parameter</td>
    </tr>
    <tr>
      <td>y</td>
      <td>int</td>
      <td>block scope</td>
    </tr>
  </tbody>
</table>

<p>A symbol table is defined and typically stored in each <a href="https://en.wikipedia.org/wiki/Single_Compilation_Unit">compilation unit</a> that corresponds to an object file (e.g. <code class="language-plaintext highlighter-rouge">*.o</code>, <code class="language-plaintext highlighter-rouge">*.obj</code>). These tables are not only used for metadata but will also serve as lookups for other object files that are trying to resolve their own symbols in the linker phase.</p>

<h3 id="slight-detour-to-undefined-reference">Slight Detour to Undefined Reference</h3>

<p>Note this is around the phase where you will get errors like <code class="language-plaintext highlighter-rouge">undefined reference</code>. The <a href="https://en.wikipedia.org/wiki/Linker_(computing)">linker</a> in these cases is trying to link the symbol references between the object files. An example of an <code class="language-plaintext highlighter-rouge">undefined reference</code>.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// This says, I promise this thing exists. Just keep compiling and </span>
<span class="c1">// it will show up when the linker searches for it.</span>
<span class="k">extern</span> <span class="kt">int</span> <span class="nf">foo</span><span class="p">();</span>

<span class="kt">int</span> <span class="nf">bar</span><span class="p">()</span> <span class="p">{</span>
  <span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="n">foo</span><span class="p">();</span>
  <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">2</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>

<p>The symbol table will be generated as…</p>

<table>
  <thead>
    <tr>
      <th>Symbol</th>
      <th>Type</th>
      <th>Scope</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>foo</td>
      <td>function, void</td>
      <td>extern (note this)</td>
    </tr>
    <tr>
      <td>bar</td>
      <td>function, int</td>
      <td>global</td>
    </tr>
    <tr>
      <td>x</td>
      <td>int</td>
      <td>block scope</td>
    </tr>
  </tbody>
</table>

<p>If <code class="language-plaintext highlighter-rouge">foo</code> isn’t included anywhere either directly or indirectly through headers, an <code class="language-plaintext highlighter-rouge">undefined reference</code> will get reported. In these cases, the linker couldn’t resolve the symbol scope, which has an <code class="language-plaintext highlighter-rouge">extern</code> placemarker waiting for linkage.</p>

<h3 id="back-on-track">Back on Track</h3>

<p>Let’s now assume the <code class="language-plaintext highlighter-rouge">extern int foo();</code> is defined in another file. The resolved symbol table becomes.</p>

<table>
  <thead>
    <tr>
      <th>Symbol</th>
      <th>Type</th>
      <th>Scope</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>foo</td>
      <td>function, void</td>
      <td>int (note this has changed)</td>
    </tr>
    <tr>
      <td>bar</td>
      <td>function, int</td>
      <td>global</td>
    </tr>
    <tr>
      <td>x</td>
      <td>int</td>
      <td>block scope</td>
    </tr>
  </tbody>
</table>

<h2 id="intermediate-representation-ir">Intermediate Representation (IR)</h2>

<p>Generated from the parser graph and symbol table is the <a href="https://en.wikipedia.org/wiki/Intermediate_representation">intermediate code / representation</a>. It is the code that gets generated before translating to assembly. The reason this exists as opposed to direct assembly translation is so that optimizations can be made before final code expression.</p>

<p>IR generators come in different types and take on different degrees of responsibility. Examples of IRs are <a href="https://en.wikipedia.org/wiki/LLVM">LLVM</a>, <a href="https://en.wikipedia.org/wiki/Java_bytecode">Java bytecode</a>, and <a href="https://en.wikipedia.org/wiki/Common_Intermediate_Language">Microsoft CIL</a>.</p>

<h3 id="net-cil">.NET CIL</h3>

<p>Applying the .NET <a href="https://docs.microsoft.com/en-us/dotnet/core/tools/">Common Intermediate Language (CIL)</a> to our canonical example below…</p>

<div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">void</span> <span class="nf">foo</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span>
  <span class="kt">int</span> <span class="n">y</span><span class="p">;</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="p">&gt;</span> <span class="m">2</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">y</span> <span class="p">=</span> <span class="m">1</span><span class="p">;</span>
  <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
    <span class="n">y</span> <span class="p">=</span> <span class="m">2</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>

<p>…becomes…</p>

<pre><code class="language-cil">.method private hidebysig instance void 
          foo(int32 x) cil managed
{
  .maxstack  2
  .locals init (int32 V_0, bool V_1)
  IL_0000:  nop
  IL_0001:  ldarg.1
  IL_0002:  ldc.i4.2
  IL_0003:  cgt
  IL_0005:  ldc.i4.0
  IL_0006:  ceq
  IL_0008:  stloc.1
  IL_0009:  ldloc.1
  IL_000a:  brtrue.s   IL_0012

  IL_000c:  nop
  IL_000d:  ldc.i4.1
  IL_000e:  stloc.0
  IL_000f:  nop
  IL_0010:  br.s       IL_0016

  IL_0012:  nop
  IL_0013:  ldc.i4.2
  IL_0014:  stloc.0
  IL_0015:  nop
  IL_0016:  ret
}
</code></pre>

<p>This is an instance of a <a href="https://en.wikipedia.org/wiki/Stack_machine">stack based</a> <a href="https://en.wikipedia.org/wiki/Bytecode">bytecode</a> representation as opposed to a <a href="https://en.wikipedia.org/wiki/Register_machine">register based approach</a>. The two approaches are not strictly one or the other, but typically a virtual machine uses one predominantly. The more popular <a href="https://en.wikipedia.org/wiki/Java_virtual_machine">JVM</a> and CIL are stack based but sometimes leverage registers. For example, the CIL will store simple constants like integers or perform calculations using registers since it <a href="https://www.intel.com/content/www/us/en/developer/articles/technical/memory-performance-in-a-nutshell.html">is faster in terms of memory access than the L1+ cache</a>.</p>

<h2 id="compiled-code">Compiled Code</h2>

<p>The last and final step is the machine code itself. This is usually the assembly code that gets ran. Using our previous example for the CIL code. This bytecode gets translated into x86 code as…</p>

<pre><code class="language-asm">C.foo(Int32)
  L0000: push ebp
  L0001: mov ebp, esp
  L0003: sub esp, 0x10
  L0006: xor eax, eax
  L0008: mov [ebp-0xc], eax
  L000b: mov [ebp-0x10], eax
  L000e: mov [ebp-4], ecx
  L0011: mov [ebp-8], edx
  L0014: cmp dword ptr [0x1adec1a8], 0
  L001b: je short L0022
  L001d: call 0x661dfc10
  L0022: nop
  L0023: cmp dword ptr [ebp-8], 2
  L0027: setg al
  L002a: movzx eax, al
  L002d: mov [ebp-0x10], eax
  L0030: cmp dword ptr [ebp-0x10], 0
  L0034: je short L0042
  L0036: nop
  L0037: mov dword ptr [ebp-0xc], 1
  L003e: nop
  L003f: nop
  L0040: jmp short L004b
  L0042: nop
  L0043: mov dword ptr [ebp-0xc], 2
  L004a: nop
  L004b: nop
  L004c: mov esp, ebp
  L004e: pop ebp
  L004f: ret
</code></pre>

<h2 id="into-the-abyss">Into the Abyss</h2>

<p>Let’s go even further now and see what the encoded values look like in the <a href="https://en.wikipedia.org/wiki/Instruction_register">instruction register</a> of an x86 CPU. On the left are the line numbers and the contents of the register, and on the right is the assembly equivalent. Notice how some of the hex matches value to the right.</p>

<pre><code class="language-asm">0:  55                      push   ebp
1:  89 e5                   mov    ebp,esp
3:  83 ec 10                sub    esp,0x10
6:  31 c0                   xor    eax,eax
8:  89 45 f4                mov    DWORD PTR [ebp-0xc],eax
b:  89 45 f0                mov    DWORD PTR [ebp-0x10],eax
e:  89 4d fc                mov    DWORD PTR [ebp-0x4],ecx
11: 89 55 f8                mov    DWORD PTR [ebp-0x8],edx
14: 83 3d a8 c1 de 1a 00    cmp    DWORD PTR ds:0x1adec1a8,0x0
1b: 74 05                   je     22 &lt;L0022&gt;
1d: e8 0c fc 1d 66          call   661dfc2e &lt;L004f+0x661dfbdf&gt;
22: 90                      nop
23: 83 7d f8 02             cmp    DWORD PTR [ebp-0x8],0x2
27: 0f 9f c0                setg   al
2a: 0f b6 c0                movzx  eax,al
2d: 89 45 f0                mov    DWORD PTR [ebp-0x10],eax
30: 83 7d f0 00             cmp    DWORD PTR [ebp-0x10],0x0
34: 74 0c                   je     42 &lt;L0042&gt;
36: 90                      nop
37: c7 45 f4 01 00 00 00    mov    DWORD PTR [ebp-0xc],0x1
3e: 90                      nop
3f: 90                      nop
40: eb 09                   jmp    4b &lt;L004b&gt;
42: 90                      nop
43: c7 45 f4 02 00 00 00    mov    DWORD PTR [ebp-0xc],0x2
4a: 90                      nop
4b: 90                      nop
4c: 89 ec                   mov    esp,ebp
4e: 5d                      pop    ebp
4f: c3                      ret
</code></pre>

<p>The byte layout from left to right is opcode, control bits, data. Imagine being the person that had to execute all of this manually.</p>

<p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/5e/Digital_PDP-8F.jpg/1920px-Digital_PDP-8F.jpg" alt="DEC PDP-8" /></p>

<p>This was considered a step up from this.</p>

<p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/fe/Used_Punchcard_%285151286161%29.jpg/1920px-Used_Punchcard_%285151286161%29.jpg" alt="IBM Punch Card" /></p>

<p>Because they didn’t want to do this.</p>

<pre><code class="language-txt">01010101
1000100111100101
100000111110110000010000
11000111000000
100010010100010111110100
100010010100010111110000
100010010100110111111100
100010010101010111111000
10000011001111011010100011000001110111100001101000000000
111010000000101
1110100000001100111111000001110101100110
10010000
10000011011111011111100000000010
11111001111111000000
11111011011011000000
100010010100010111110000
10000011011111011111000000000000
111010000001100
10010000
11000111010001011111010000000001000000000000000000000000
10010000
10010000
1110101100001001
10010000
11000111010001011111010000000010000000000000000000000000
10010000
10010000
1000100111101100
01011101
11000011
</code></pre>

<p>In fact the whole concept of an assembler was once considered by some as a wasteful endevour. It was using computational resources to create instructions to use computational resources. The whole point is that it used computational resources! In today’s world where we are pretty wasteful of computational resources by orders of magnitude, this was not taken for granted back then when the idea of <a href="https://en.wikipedia.org/wiki/Time-sharing">computational time sharing</a> was a thing. We have come a long way.</p>

<h2 id="closing-thoughts">Closing Thoughts</h2>

<p>So now we have come full circle from…</p>

<div class="language-csharp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">void</span> <span class="nf">foo</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span>
  <span class="kt">int</span> <span class="n">y</span><span class="p">;</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="p">&gt;</span> <span class="m">2</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">y</span> <span class="p">=</span> <span class="m">1</span><span class="p">;</span>
  <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
    <span class="n">y</span> <span class="p">=</span> <span class="m">2</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>

<p>…to the binary seen above.</p>

<p>The compiler is a great thing, it allows us to use abstractions that make development much easier and express ideas in ways that humans are more able to grasp. Turning the realm of controlled electrons into that of the symbolic and philosophical, permitting us to process and interpret the world around us.</p>

<h2 id="references--more-reading">References &amp; More Reading</h2>

<ul>
  <li>To start exploring on how to create a language for yourself, check out <a href="https://www.antlr.org/">Antlr</a> (lexer) and <a href="https://www.gnu.org/software/bison/">Bison</a> (parser).</li>
  <li><a href="https://llvm.org/docs/tutorial/MyFirstLanguageFrontend/index.html">My First Language Frontend with LLVM Tutorial</a> - Exactly what it says.</li>
  <li>Lecture notes on <a href="https://cop3402fall20.github.io/lectures/03_toy_compiler.html">Writing a Toy Compiler</a>.</li>
  <li><a href="https://www.twilio.com/blog/abstract-syntax-trees">ASTs - What they are and how to use them</a> - Engaging blog post on how ASTs work and dives further into other topics also covered here.</li>
  <li><a href="https://godbolt.org/">GodBolt: Compiler Explorer</a> - Fantastic online IDE to view compiled assembly from high level languages.</li>
  <li><a href="http://web.eecs.utk.edu/~azh/blog/teenytinycompiler1.html">Let’s make a Teeny Tiny compiler, part 1</a> - First part of a series showing how to make a compiler using Python. Trivial but approachable and demonstrative to learning compilers.</li>
</ul>]]></content><author><name>Andrew Walsh</name><email>awalsh128@gmail.com</email></author><category term="Compilers" /><summary type="html"><![CDATA[Introduction of compiler basics using a toy example.]]></summary></entry><entry><title type="html">Doxygen Themes</title><link href="https://awalsh128.com/doxygen-themes-release/" rel="alternate" type="text/html" title="Doxygen Themes" /><published>2021-10-23T00:00:00+00:00</published><updated>2021-10-23T07:00:01+00:00</updated><id>https://awalsh128.com/doxygen-themes-release</id><content type="html" xml:base="https://awalsh128.com/doxygen-themes-release/"><![CDATA[<p>I just released a new project on GitHub called <a href="https://github.com/awalsh128/doxygen-themes">doxygen-themes</a> which allows users to customize Doxygen styling and colors using some example templates, including a stylesheet boiled down to a few color variables.</p>

<p><a href="https://github.com/awalsh128/doxygen-themes/CONTRIBUTING.md">Contributions</a> are definitely welcome. For the lazy, here is the README.md below.</p>

<p>Some sample Doxygen themes using Bootstrap 5 with different approaches to CSS use. Not all of the HTML is Bootstrap though, just the header and footer with Bootstrap like coloring applied to the body elements.</p>

<hr />

<p><a href="https://www.awalsh128.com/doxygen-themes/">Click here for the demo landing page.</a>.</p>

<hr />

<h2 id="documentation">Documentation</h2>

<p>This project is a demonstration of possible approaches to theming Doxygen generated websites. Frameworks other than Bootstrap can be used, this is just an example of what is possible.</p>

<ul>
  <li>Applied directly.</li>
  <li>Root variables applied.</li>
  <li>Simple color palette propagated.</li>
</ul>

<h2 id="pre-requisites">Pre-requisites</h2>

<ul>
  <li>CMake - Build the project and create the website.</li>
  <li>Doxygen - Create the website documentation.</li>
</ul>

<h2 id="components">Components</h2>

<p>All components use CDN links to keep project file overhead low.</p>

<ul>
  <li>Bootstrap - Styles the header and footer. Also provides the primary colors for the Doxygen classes.</li>
  <li>Font Awesome - Header icons.</li>
  <li>jQuery - Required by Bootstrap and used for the custom palette picker.</li>
</ul>

<h2 id="sample-css">Sample CSS</h2>

<p>Below is the CSS used for the Neon Pink theme.</p>

<div class="language-css highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nd">:root</span> <span class="p">{</span>
  <span class="py">--color-body</span><span class="p">:</span> <span class="m">#bda9a9</span><span class="p">;</span>
  <span class="py">--color-hyperlink</span><span class="p">:</span> <span class="m">#ffffff</span><span class="p">;</span>
  <span class="py">--color-title-background</span><span class="p">:</span> <span class="m">#8a1253</span><span class="p">;</span>
  <span class="py">--color-title-text</span><span class="p">:</span> <span class="m">#ffffff</span><span class="p">;</span>
  <span class="py">--color-footer-header-background</span><span class="p">:</span> <span class="m">#c51350</span><span class="p">;</span>
  <span class="py">--color-footer-header-text</span><span class="p">:</span> <span class="m">#ffffff</span><span class="p">;</span>
  <span class="py">--color-section-header-background</span><span class="p">:</span> <span class="m">#e8751a</span><span class="p">;</span>
  <span class="py">--color-section-header-text</span><span class="p">:</span> <span class="m">#ffffff</span><span class="p">;</span>
  <span class="py">--color-section-subheader-background</span><span class="p">:</span> <span class="m">#fda403</span><span class="p">;</span>
  <span class="py">--color-section-subheader-text</span><span class="p">:</span> <span class="m">#ffffff</span><span class="p">;</span>
  <span class="py">--color-section-text</span><span class="p">:</span> <span class="m">#ffffff</span><span class="p">;</span>
  <span class="py">--color-section-background</span><span class="p">:</span> <span class="m">#bd9a9a</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>

<p>Notice how there is a lot of <code class="language-plaintext highlighter-rouge">#ffffff</code> in there. This could probably be further simplified.</p>

<h3 id="future-ideas">Future Ideas</h3>

<p>There are a lot of CSS features <a href="https://developer.mozilla.org/en-US/docs/Web/CSS">that can be found</a>. Some ways to simplify the palette even more.</p>

<ul>
  <li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/filter-function">Filter Functions</a> - Allows for different affects and interesting functions like <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/filter-function/brightness()">brightness</a>.</li>
  <li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hsl()">HSL Scheme</a> - Makes darkening and lightening easier.</li>
  <li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/calc()">Arithmetic</a> - Useful for modifying color values.</li>
</ul>]]></content><author><name>Andrew Walsh</name><email>awalsh128@gmail.com</email></author><category term="Bootstrap" /><category term="Doxygen" /><summary type="html"><![CDATA[Customizable and modernized Doxygen themes.]]></summary></entry><entry><title type="html">Automated Testing for GitHub Actions</title><link href="https://awalsh128.com/automated-testing-for-github-actions/" rel="alternate" type="text/html" title="Automated Testing for GitHub Actions" /><published>2021-10-12T00:00:00+00:00</published><updated>2021-10-16T07:00:01+00:00</updated><id>https://awalsh128.com/automated-testing-for-github-actions</id><content type="html" xml:base="https://awalsh128.com/automated-testing-for-github-actions/"><![CDATA[<p>GitHub actions don’t support running tests on created actions from the same repository. In order to do this, another repository needs to be created. There is a way to test locally using <a href="https://github.com/nektos/act">nektos/act</a> but this doesn’t address code being committed upstream and continuously integrated. Let’s get to it.</p>

<p>For this walkthrough, there will be two endpoints:</p>

<ul>
  <li>Action repository where your created action under test lives.</li>
  <li>Test repository that houses the tests.</li>
</ul>

<p>The general idea is to have your action repository publish an event that says a push has occurred, relay it to the test repository which triggers tests on the test repository. For this example I am using my own recently published action <code class="language-plaintext highlighter-rouge">awalsh128/cache-apt-pkgs-action</code>. Feel free to swap out this with your equivalent action.</p>

<p>The steps needed are to create:</p>

<ul>
  <li>a shared secret,</li>
  <li>a staging (or another) branch besides master on the action repository,</li>
  <li>a repository to run tests on your action code,</li>
  <li>tests in your test repository,</li>
  <li>a publish action in your action repository,</li>
  <li>a subscriber action in your test repository.</li>
</ul>

<p>More details on each step below.</p>

<h2 id="create-shared-secret">Create Shared Secret</h2>

<p>A Personal Access Token (PAT) will allow the publisher (action repository) to trigger the event that the subscriber (test repository) will then act on. This shared secret will be needed so they can interact.</p>

<p><a href="https://github.com/settings/tokens">Instructions to setup your PAT. Use the arguments below.</a></p>

<p><code class="language-plaintext highlighter-rouge">Profile &gt; Settings &gt; Developer settings &gt; Personal Access Token &gt; Generate New Token</code></p>

<table>
  <thead>
    <tr>
      <th>Field</th>
      <th>Value</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Note</td>
      <td>Publish push requests to awalsh128/cache-apt-pkgs-action-ci</td>
    </tr>
    <tr>
      <td>Expiration</td>
      <td>(whatever you choose this to be)</td>
    </tr>
    <tr>
      <td>Access:</td>
      <td><code class="language-plaintext highlighter-rouge">repo_public</code></td>
    </tr>
  </tbody>
</table>

<p><img src="/assets/img/2021-10-12-automated-testing-for-github-actions/personal_access_token.png" alt="Example PAT Config Screen" /></p>

<p>Once you complete the configuration it will then show you what the value is. Note this isn’t my actual value, so don’t get any ideas. &gt;:|</p>

<p><img src="/assets/img/2021-10-12-automated-testing-for-github-actions/personal_access_token_value.png" alt="Example PAT Value Screen" /></p>

<p>We can now share this PAT / secret with the publisher.</p>

<h2 id="publisher">Publisher</h2>

<p>Turning our attention to the publisher side (action repository) of the event that will trigger the tests on the test repository.</p>

<h3 id="store-shared-secret-on-publisher">Store Shared Secret on Publisher</h3>

<p>In order for the action repository to publish the event to the test repository, it will need access to it. The PAT will need to be stored in a variable so it can be used in the action without actually revealing it’s value.</p>

<p>To create a scecret, you can use a URL like below. Replace the <code class="language-plaintext highlighter-rouge">cache-apt-pkgs-action-ci</code> repository with your own test repository name.</p>

<p><a href="https://github.com/awalsh128/cache-apt-pkgs-action-ci/settings/secrets/actions/new">https://github.com/awalsh128/cache-apt-pkgs-action-ci/settings/secrets/actions/new</a></p>

<p>Use the arguments below when creating the secret.</p>

<p><code class="language-plaintext highlighter-rouge">Settings &gt; Secrets &gt; New repository secret</code></p>

<table>
  <thead>
    <tr>
      <th>Field</th>
      <th>Value</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Name</td>
      <td>TRIGGER_PUBLISH_STAGING_PR_TOKEN</td>
    </tr>
    <tr>
      <td>Secret</td>
      <td>(value taken from Create Shared Secret)</td>
    </tr>
  </tbody>
</table>

<p>Here’s an example of the PAT we generated before.</p>

<p><img src="/assets/img/2021-10-12-automated-testing-for-github-actions/secret.png" alt="Example PAT Value Screen" /></p>

<h3 id="create-a-staging-branch-in-awalsh128cache-apt-pkgs-action">Create a <code class="language-plaintext highlighter-rouge">staging</code> branch in <code class="language-plaintext highlighter-rouge">awalsh128/cache-apt-pkgs-action</code></h3>

<p>This will be used as a testing branch. Broken code can live here without going to <code class="language-plaintext highlighter-rouge">master</code>. It doesn’t have to be called <code class="language-plaintext highlighter-rouge">staging</code>, call it whatever you want (e.g. <code class="language-plaintext highlighter-rouge">dev</code>, <code class="language-plaintext highlighter-rouge">whatever</code>).</p>

<div class="language-sh highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git checkout <span class="nt">-b</span> staging
git push origin staging
git push <span class="nt">--set-upstream</span> origin staging
</code></pre></div></div>

<h3 id="create-publish-action">Create Publish Action</h3>

<p>In the action repository, create <code class="language-plaintext highlighter-rouge">.github/workflows/staging_push.yml</code> workflow that will trigger on any push to staging.</p>

<div class="language-yml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="na">name</span><span class="pi">:</span> <span class="s">Publish Staging Push Event</span>
<span class="na">on</span><span class="pi">:</span>
  <span class="c1"># Allow manual triggering for debugging.</span>
  <span class="na">workflow_dispatch</span><span class="pi">:</span>
  <span class="c1"># Publish when we see a push to staging.</span>
  <span class="na">push</span><span class="pi">:</span>
    <span class="na">branches</span><span class="pi">:</span>
      <span class="pi">-</span> <span class="s">staging</span>

<span class="na">jobs</span><span class="pi">:</span>
  <span class="na">publish_event</span><span class="pi">:</span>
    <span class="na">runs-on</span><span class="pi">:</span> <span class="s">ubuntu-latest</span>
    <span class="na">name</span><span class="pi">:</span> <span class="s">Publish staging push</span>
    <span class="na">steps</span><span class="pi">:</span>
      <span class="c1"># Note the event_type and URL secrets passed so the action repository </span>
      <span class="c1"># is allowed to post to the test repository.</span>
      <span class="pi">-</span> <span class="na">run</span><span class="pi">:</span> <span class="pi">|</span>
          <span class="s">curl -i \</span>
            <span class="s">-X POST \</span>
            <span class="s">-H "Accept: application/vnd.github.v3+json" \</span>
            <span class="s">-H "Authorization: token ${{ secrets.PUBLISH_PUSH_TOKEN }}" \</span>
            <span class="s">https://api.github.com/repos/awalsh128/cache-apt-pkgs-action-ci/dispatches \</span>
            <span class="s">-d '{"event_type":"staging_push"}'</span>
</code></pre></div></div>

<h2 id="subscriber">Subscriber</h2>

<p>Now on the subscriber (test repository), setup the tests and trigger to respond to the staging push.</p>

<h3 id="create-repository-and-test">Create Repository and Test</h3>

<ul>
  <li>Create <code class="language-plaintext highlighter-rouge">awalsh128/cache-apt-pkgs-action-ci</code> repository (ci = continuous integration) for testing.</li>
  <li>Create a workflow <code class="language-plaintext highlighter-rouge">.github/workflows/tests.yml</code> that subscribes to <code class="language-plaintext highlighter-rouge">staging_push</code> events and runs tests.</li>
</ul>

<div class="language-yml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="na">name</span><span class="pi">:</span> <span class="s">Staging Push Tests</span>
<span class="na">on</span><span class="pi">:</span>
  <span class="c1"># Allow for manual dispatches so we can test the workflow if needed.</span>
  <span class="na">workflow_dispatch</span><span class="pi">:</span>
  <span class="na">repository_dispatch</span><span class="pi">:</span>
    <span class="c1"># Name of the event that will by pubsub'd.</span>
    <span class="na">types</span><span class="pi">:</span> <span class="pi">[</span><span class="nv">staging_push</span><span class="pi">]</span>

<span class="na">jobs</span><span class="pi">:</span>
  <span class="na">install</span><span class="pi">:</span>
    <span class="na">runs-on</span><span class="pi">:</span> <span class="s">ubuntu-latest</span>
    <span class="na">name</span><span class="pi">:</span> <span class="s">Install and cache.</span>
    <span class="na">steps</span><span class="pi">:</span>
      <span class="pi">-</span> <span class="na">uses</span><span class="pi">:</span> <span class="s">actions/checkout@v2</span>
      <span class="c1"># Note that the action uses the @staging version.</span>
      <span class="c1"># Allows testing to happen on that branch so it can get pulled into master once it passes.</span>
      <span class="pi">-</span> <span class="na">uses</span><span class="pi">:</span> <span class="s">awalsh128/cache-apt-pkgs-action@staging</span>
        <span class="na">with</span><span class="pi">:</span>
          <span class="na">packages</span><span class="pi">:</span> <span class="s">xdot rolldice</span>
</code></pre></div></div>

<p>That’s it. Now anytime code is pushed to staging on your action repository, it will trigger tests on your test repository.</p>

<h2 id="more-resources">More Resources</h2>

<ul>
  <li><a href="https://docs.github.com/en/rest/reference/repos#create-a-repository-dispatch-event">Github: Create Repository Dispatch Event</a></li>
  <li><a href="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token">Github: Create Personal Access Token</a></li>
  <li><a href="https://blog.m157q.tw/posts/2020/07/16/make-one-github-actions-workflow-trigger-another-github-actions-workflow">Trigger Github Actions between Repositories</a></li>
</ul>]]></content><author><name>Andrew Walsh</name><email>awalsh128@gmail.com</email></author><category term="Testing" /><category term="GitHub" /><summary type="html"><![CDATA[Create a testing harness for your created GitHub Actions.]]></summary></entry><entry><title type="html">Code Analysis for GitHub Projects</title><link href="https://awalsh128.com/integrating-lgtm/" rel="alternate" type="text/html" title="Code Analysis for GitHub Projects" /><published>2021-10-10T00:00:00+00:00</published><updated>2021-10-10T07:00:01+00:00</updated><id>https://awalsh128.com/integrating-lgtm</id><content type="html" xml:base="https://awalsh128.com/integrating-lgtm/"><![CDATA[<p>LGTM is a nice little integration for your GitHub projects that performs code analysis and alerts if any violations are found. And example of what an alert screen looks like on the LGTM site below (using my fluentcpp project as a demonstration).</p>

<p><img src="/assets/img/2021-10-10-integrating-lgtm/alert_screen.png" alt="Example LGTM Alert Screen" /></p>

<p>You will need to grant the application access to your repositories using their guide <a href="https://lgtm.com/help/lgtm/github-apps-integration">Integration with GitHub Apps</a>.</p>

<p>You can then setup badges on your project site that show the status of your code quality.</p>

<p><a href="https://lgtm.com/projects/g/awalsh128/fluentcpp/context:cpp"><img src="https://img.shields.io/lgtm/grade/cpp/g/awalsh128/fluentcpp.svg?logo=lgtm&amp;logoWidth=18" alt="Code Grade" /></a>
<a href="https://lgtm.com/projects/g/awalsh128/fluentcpp/alerts/"><img src="https://img.shields.io/lgtm/alerts/g/awalsh128/fluentcpp.svg?logo=lgtm&amp;logoWidth=18" alt="Code Alerts" /></a></p>

<p>If more customization is needed, an <code class="language-plaintext highlighter-rouge">.lgtml.yml</code> file can be created in the root of the repository per <a href="https://lgtm.com/help/lgtm/lgtm.yml-configuration-file">their instructions</a>. You can also download <a href="https://lgtm.com/static/downloads/lgtm.template.yml">their canonical template</a> too to get started.</p>

<p>Here’s <a href="https://github.com/search?q=filename%3A.lgtm.yml&amp;type=Code">a search of GitHub</a> to look at other example configuration files. For example in my project I needed a more recent version of CMake. This is what mine looks like so it is in the environment before LGTM performs its analysis.</p>

<div class="language-yml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="na">extraction</span><span class="pi">:</span>
  <span class="na">cpp</span><span class="pi">:</span>
    <span class="na">after_prepare</span><span class="pi">:</span>
      <span class="pi">-</span> <span class="s2">"</span><span class="s">mkdir</span><span class="nv"> </span><span class="s">custom_cmake"</span>
      <span class="c1"># Need later version of CMake than lgtm has.</span>
      <span class="pi">-</span> <span class="s2">"</span><span class="s">wget</span><span class="nv"> </span><span class="s">--quiet</span><span class="nv"> </span><span class="s">-O</span><span class="nv"> </span><span class="s">-</span><span class="nv"> </span><span class="s">https://cmake.org/files/v3.16/cmake-3.16.3-Linux-x86_64.tar.gz</span><span class="nv"> </span><span class="s">|</span><span class="nv"> </span><span class="s">tar</span><span class="nv"> </span><span class="s">--strip-components=1</span><span class="nv"> </span><span class="s">-xz</span><span class="nv"> </span><span class="s">-C</span><span class="nv"> </span><span class="s">custom_cmake"</span>
      <span class="pi">-</span> <span class="s2">"</span><span class="s">export</span><span class="nv"> </span><span class="s">PATH=$(pwd)/custom_cmake/bin:${PATH}"</span>
    <span class="na">index</span><span class="pi">:</span>
      <span class="na">build_command</span><span class="pi">:</span>
        <span class="pi">-</span> <span class="s">cd $LGTM_SRC</span>
        <span class="pi">-</span> <span class="s">mkdir build; cd build</span>
        <span class="pi">-</span> <span class="s">cmake .. -DCMAKE_BUILD_TYPE=RELWITHDEBINFO</span>
        <span class="pi">-</span> <span class="s">make</span>
</code></pre></div></div>

<p>Here’s another <a href="https://github.com/rafifm/olshop_ci4/blob/ce1b0095ab3e45e373f05d143374ad9a941054f4/public/themes/.lgtm.yml">taken from GitHub</a> that excludes specified directories from analysis.</p>

<div class="language-yml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="na">path_classifiers</span><span class="pi">:</span>
  <span class="na">plugins</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">plugins/</span>

<span class="na">extraction</span><span class="pi">:</span>
  <span class="na">javascript</span><span class="pi">:</span>
    <span class="c1"># https://lgtm.com/help/lgtm/javascript-extraction#customizing-index</span>
    <span class="c1"># The `index` step extracts information from the files in the codebase.</span>
    <span class="na">index</span><span class="pi">:</span>
      <span class="c1"># Specify a list of files and folders to exclude from extraction.</span>
      <span class="na">exclude</span><span class="pi">:</span>
        <span class="pi">-</span> <span class="s">bower_components/</span>
        <span class="pi">-</span> <span class="s">docs/assets/js/plugins/</span>
        <span class="pi">-</span> <span class="s">plugins/</span>
</code></pre></div></div>

<p>… or <a href="https://github.com/Johncrown56/News-App/blob/d7c3255d744036cf4d73655b2f3e3723cad3bdff/node_modules/regextras/lgtm.yml">from another repository</a> more simply.</p>

<div class="language-yml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="na">extraction</span><span class="pi">:</span>
  <span class="na">javascript</span><span class="pi">:</span>
    <span class="na">index</span><span class="pi">:</span>
      <span class="na">filters</span><span class="pi">:</span>
        <span class="pi">-</span> <span class="na">exclude</span><span class="pi">:</span> <span class="s2">"</span><span class="s">dist"</span>
</code></pre></div></div>

<p>Here’s yet <a href="https://github.com/CPTX1/Curl/blob/3027f06078f89232251eb1c921f6142894982ea1/curl-master/.lgtm.yml">another repository</a>. This one disables CMake and sets the configuration options before build.</p>

<div class="language-yml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="na">extraction</span><span class="pi">:</span>
  <span class="na">cpp</span><span class="pi">:</span>
    <span class="na">prepare</span><span class="pi">:</span>
      <span class="na">packages</span><span class="pi">:</span> <span class="c1"># to avoid confusion with libopenafs-dev which also provides a des.h</span>
        <span class="pi">-</span> <span class="s">libssl-dev</span>
    <span class="na">after_prepare</span><span class="pi">:</span> <span class="c1"># make sure lgtm.com doesn't use CMake (which generates and runs tests)</span>
      <span class="pi">-</span> <span class="s">rm -f CMakeLists.txt</span>
      <span class="pi">-</span> <span class="s">./buildconf</span>
    <span class="na">configure</span><span class="pi">:</span> <span class="c1"># enable as many optional features as possible</span>
      <span class="na">command</span><span class="pi">:</span> <span class="s">./configure --enable-ares --with-libssh2 --with-gssapi --with-librtmp --with-libmetalink --with-libmetalink</span>
</code></pre></div></div>

<p>I think you get the point. Just give the app permission, customize as needed with the <code class="language-plaintext highlighter-rouge">.lgtml.yml</code> and setup your cute badges.</p>]]></content><author><name>Andrew Walsh</name><email>awalsh128@gmail.com</email></author><category term="Code Analysis" /><category term="GitHub" /><summary type="html"><![CDATA[Integrate your GitHub projects with a code analysis tool.]]></summary></entry><entry><title type="html">Fluent C++ v0.1 Release</title><link href="https://awalsh128.com/fluentcpp-v01-release/" rel="alternate" type="text/html" title="Fluent C++ v0.1 Release" /><published>2021-10-09T00:00:00+00:00</published><updated>2021-10-10T07:00:01+00:00</updated><id>https://awalsh128.com/fluentcpp-v01-release</id><content type="html" xml:base="https://awalsh128.com/fluentcpp-v01-release/"><![CDATA[<h2 id="motivation">Motivation</h2>

<p>I had been wanting to make a library to express data transformation in a fluent way. The STL already has some functions for this. For example, if I wanted to transform a sequence and then filter based on a conditional I would do this.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">xs</span><span class="p">;</span>
<span class="c1">// Add int to xs.</span>
<span class="n">std</span><span class="o">::</span><span class="n">transform</span><span class="p">(</span>
  <span class="n">xs</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="n">xs</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span>
  <span class="p">[](</span><span class="k">auto</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="o">++</span><span class="n">x</span><span class="p">;</span> <span class="p">});</span>

<span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">filtered</span><span class="p">;</span>
<span class="n">filtered</span><span class="p">.</span><span class="n">reserve</span><span class="p">(</span><span class="n">xs</span><span class="p">.</span><span class="n">size</span><span class="p">());</span>
<span class="n">std</span><span class="o">::</span><span class="n">copy_if</span><span class="p">(</span>
  <span class="n">xs</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span> <span class="n">xs</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span>
  <span class="n">std</span><span class="o">::</span><span class="n">back_inserter</span><span class="p">(</span><span class="n">filtered</span><span class="p">),</span>
  <span class="p">[](</span><span class="k">auto</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">x</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">;</span> <span class="p">});</span>
</code></pre></div></div>

<p>This is indeed a functional approach to expressing data transformations. It applies functions to data sets and without side effects (as long as you don’t cause any in the lambda). Although, there can also be a lot of boilerplate if you just wants the entire range of the container. Also, it is not always obvious from first glance what the inputs and outputs are to the function because it doesn’t follow the typical <code class="language-plaintext highlighter-rouge">f(a,b) = c</code> mathematical format. This doesn’t make it less functional but perhaps less readable.</p>

<p>The library that I put together uses a different approach that addresses some of these concerns.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;fluentcpp/query.h&gt;</span><span class="cp">
</span>
<span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">xs</span><span class="p">;</span>
<span class="c1">// Add int to xs.</span>
<span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">filtered</span> <span class="o">=</span>
  <span class="n">fcpp</span><span class="o">::</span><span class="n">query</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">xs</span><span class="p">))</span>
    <span class="p">.</span><span class="n">select</span><span class="p">([](</span><span class="k">auto</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="o">++</span><span class="n">x</span><span class="p">;</span> <span class="p">})</span>
    <span class="p">.</span><span class="n">where</span><span class="p">([](</span><span class="k">auto</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">x</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">;</span> <span class="p">});</span>
</code></pre></div></div>

<p>This style is patterned after the <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/">.NET LINQ framework</a> something I had become very accustomed to before programming in C++ full time.</p>

<h2 id="initial-release">Initial Release</h2>

<p><a href="https://github.com/awalsh128/fluentcpp/releases/tag/v0.1">Version 0.1 is now available on GitHub</a>. As mentioned in the repository <a href="https://github.com/awalsh128/fluentcpp#installing">README.md</a>, you can easily install the library with the instructions below.</p>

<ul>
  <li>Go to <a href="https://github.com/awalsh128/fluentcpp/releases">the release</a> you want to install.</li>
  <li>Download the <code class="language-plaintext highlighter-rouge">fluentcpp-&lt;tag&gt;.tar.gz</code> file.</li>
  <li>Decompress the file in the directory <code class="language-plaintext highlighter-rouge">tar -xvzf fluentcpp-&lt;tag&gt;.tar.gz</code>.</li>
  <li>Run the install command <code class="language-plaintext highlighter-rouge">sudo ./install.sh</code>.</li>
  <li>The library files are now installed on your system and can be used as in the examples.</li>
</ul>

<h2 id="caveats">Caveats</h2>

<p>Since this is an initial release, there are some drawbacks to using this over the STL functional library or just older C++ constructs (e.g. for-loops).</p>

<ul>
  <li>Memory semantics are strictly by reference and move. This is intentional to accomodate the strictest object constructor and assignment declarations (i.e. no copy and move).</li>
  <li>Strict memory semantics has implications for fundamental types where copy is more efficient.</li>
  <li>Performance has not been benchmarked yet so it is unknown how it performs against the alternatives.</li>
  <li>Static analysis hints can still seem obscure. While they depend on the STL <a href="https://en.cppreference.com/w/cpp/concepts">concepts</a> enforced, the wording can be confusing across the interface.</li>
</ul>

<h2 id="feedback">Feedback</h2>

<p>I hope this proves useful to people and can become a wider community effort. It is primarily a proof of concept work and needs more to demonstrate it can be used in production code with similar performance to the status quo.</p>]]></content><author><name>Andrew Walsh</name><email>awalsh128@gmail.com</email></author><category term="Fluent C++" /><category term="C++" /><category term="fluent programming" /><summary type="html"><![CDATA[Fluent style querying for C++.]]></summary></entry><entry><title type="html">CMake Simple Library Example</title><link href="https://awalsh128.com/cmake-simple-library/" rel="alternate" type="text/html" title="CMake Simple Library Example" /><published>2021-10-07T00:00:00+00:00</published><updated>2021-10-10T07:00:01+00:00</updated><id>https://awalsh128.com/cmake-simple-library</id><content type="html" xml:base="https://awalsh128.com/cmake-simple-library/"><![CDATA[<h2 id="motivation">Motivation</h2>

<p>There are already some tutorials and examples of setting up a CMake project already but I work very much ny example and then deep dive later. It’s just how my brain works I guess. So let’s skim the waters.</p>

<h2 id="assumptions">Assumptions</h2>

<p>This example uses the follow dev stack.</p>

<ul>
  <li>CMake (obviously)</li>
  <li>C++ code.</li>
  <li>Catch2 testing framework (<a href="https://github.com/catchorg/Catch2">GitHub</a>).</li>
</ul>

<p>The file layout has subdirectory for source code and tests, each with their own CMake file and one at the root level.</p>

<pre><code class="language-txt">CMakeLists.txt
tests
├── CMakeLists.txt
└── mylib_test.cpp
src
├── CMakeLists.txt
├── mylib.h
└── mylib.h
</code></pre>

<h2 id="configuration">Configuration</h2>

<p>Here is the whole configuration in its respective files.</p>

<p><code class="language-plaintext highlighter-rouge">/CMakeLists.txt</code></p>

<div class="language-cmake highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">cmake_minimum_required</span><span class="p">(</span>VERSION 3.16.3<span class="p">)</span>

<span class="nb">include</span><span class="p">(</span>GNUInstallDirs<span class="p">)</span>     <span class="c1"># Make CMAKE_INSTALL_*DIR variables available.</span>
<span class="nb">set</span><span class="p">(</span>CMAKE_CXX_STANDARD 20<span class="p">)</span>  <span class="c1"># Use standard C++20 for compilation.</span>

<span class="nb">project</span><span class="p">(</span>mylib VERSION 0.1 DESCRIPTION <span class="s2">"My library is a library that you can use as a library."</span><span class="p">)</span>

<span class="c1"># Make the src directory available for include lookup.</span>
<span class="nb">include_directories</span><span class="p">(</span>src<span class="p">)</span>

<span class="c1"># Tell CMake to include the /src/CMakeLists.txt file in the build.</span>
<span class="nb">add_subdirectory</span><span class="p">(</span>src<span class="p">)</span>

<span class="c1"># Must live in the top level file or tests won't be found.</span>
<span class="nb">include</span><span class="p">(</span>CTest<span class="p">)</span>  <span class="c1"># Automatically invokes enable_testing()</span>
<span class="c1"># Tell CMake to include the /tests/CMakeLists.txt file in the build.</span>
<span class="nb">add_subdirectory</span><span class="p">(</span>tests<span class="p">)</span>
</code></pre></div></div>

<p><code class="language-plaintext highlighter-rouge">/src/CMakeLists.txt</code></p>

<div class="language-cmake highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">message</span><span class="p">(</span>STATUS <span class="s2">"Building and installing library."</span><span class="p">)</span>

<span class="c1"># Add library to build.</span>
<span class="c1">#</span>
<span class="c1"># STATIC rolls all code (including related includes) into a single compiled object.</span>
<span class="c1"># SHARED rolls only mylib code into a single compiled object.</span>
<span class="c1">#</span>
<span class="c1"># https://stackoverflow.com/questions/2649334/difference-between-static-and-shared-libraries</span>
<span class="c1">#</span>
<span class="c1"># If you are using a different type of library than STATIC, make sure to look into how the target properties and </span>
<span class="c1"># install will be different. For example https://cmake.org/cmake/help/latest/command/install.html</span>
<span class="c1">#</span>
<span class="nb">add_library</span><span class="p">(</span>mylib STATIC mylib.h mylib.cpp<span class="p">)</span>

<span class="nb">set_target_properties</span><span class="p">(</span>mylib PROPERTIES
  VERSION <span class="si">${</span><span class="nv">PROJECT_VERSION</span><span class="si">}</span>  <span class="c1"># Already set in the parent CMakeLists.txt via 'project'.</span>
  PUBLIC_HEADER mylib.h       <span class="c1"># Pulic headers you intend other projects to include.</span>
  CXX_STANDARD_REQUIRED 20<span class="p">)</span>   <span class="c1"># Enforce the C++20 standard to compile against.</span>

<span class="nb">install</span><span class="p">(</span>TARGETS mylib
  <span class="c1"># Used for STATIC library headers.</span>
  ARCHIVE DESTINATION <span class="si">${</span><span class="nv">CMAKE_INSTALL_LIBDIR</span><span class="si">}</span> COMPONENT lib
  <span class="c1"># Rolled code lives here.</span>
  PUBLIC_HEADER DESTINATION <span class="si">${</span><span class="nv">CMAKE_INSTALL_INCLUDEDIR</span><span class="si">}</span>/mylib COMPONENT dev<span class="p">)</span>
</code></pre></div></div>

<p><code class="language-plaintext highlighter-rouge">/tests/CMakeLists.txt</code></p>

<div class="language-cmake highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">include</span><span class="p">(</span>FetchContent<span class="p">)</span>

<span class="nb">message</span><span class="p">(</span>STATUS <span class="s2">"Building tests."</span><span class="p">)</span>

<span class="c1"># Pull in the Catch2 framework.</span>
<span class="nf">FetchContent_Declare</span><span class="p">(</span>
  Catch2
  GIT_REPOSITORY https://github.com/catchorg/Catch2.git
  GIT_TAG        v3.0.0-preview3<span class="p">)</span>
<span class="nf">FetchContent_MakeAvailable</span><span class="p">(</span>Catch2<span class="p">)</span>

<span class="c1"># Most all test frameworks create a binary that then runs the tests.</span>
<span class="c1"># This is a simple expression of that and nothing special to tests themselves.</span>
<span class="c1">#</span>
<span class="c1"># In this framework, Catch2 provides a main function as executable entry point.</span>
<span class="nb">add_executable</span><span class="p">(</span>query_tests mylib_test.cpp<span class="p">)</span>

<span class="c1"># Link the included libraries in for the tests.</span>
<span class="nb">target_link_libraries</span><span class="p">(</span>my_tests PUBLIC Catch2 Catch2WithMain mylib<span class="p">)</span>

<span class="nb">add_test</span><span class="p">(</span>NAME tests COMMAND query_tests<span class="p">)</span>
</code></pre></div></div>

<h2 id="more-resources">More Resources</h2>

<p>Here are some more examples to help you along.</p>

<ul>
  <li><a href="https://github.com/dev-cafe/cmake-cookbook">Dev Cafe: CMake Cookbook Examples</a></li>
  <li><a href="https://github.com/search?q=filename%3ACMakeLists.txt">GitHub: CMakeLists.txt Search</a></li>
  <li>CMake Documentation
    <ul>
      <li><a href="https://cmake.org/cmake/help/latest/command/add_library.html">add_library</a></li>
      <li><a href="https://cmake.org/cmake/help/latest/command/add_test.html">add_test</a></li>
      <li><a href="https://cmake.org/cmake/help/latest/command/install.html">install</a></li>
      <li><a href="https://cmake.org/cmake/help/latest/command/set_target_properties.html">set_target_properties</a></li>
    </ul>
  </li>
</ul>]]></content><author><name>Andrew Walsh</name><email>awalsh128@gmail.com</email></author><category term="CMake" /><summary type="html"><![CDATA[An example of a simple library using CMake.]]></summary></entry><entry><title type="html">GitHub Actions - Cache APT Packages v1 Released</title><link href="https://awalsh128.com/github-actions-cache-apt-pkgs-v1-released/" rel="alternate" type="text/html" title="GitHub Actions - Cache APT Packages v1 Released" /><published>2021-09-18T00:00:00+00:00</published><updated>2021-10-10T07:00:01+00:00</updated><id>https://awalsh128.com/github-actions-cache-apt-pkgs-v1-released</id><content type="html" xml:base="https://awalsh128.com/github-actions-cache-apt-pkgs-v1-released/"><![CDATA[<p>I created a GitHub action <a href="https://github.com/marketplace/actions/cache-apt-packages">awalsh128/cache-apt-packages</a> that allows caching of Advanced Package Tool (APT) package dependencies to improve workflow execution time instead of installing the packages on every run.</p>

<p>Here is an example using it to cache Doxygen dependencies.</p>

<div class="language-yaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="na">name</span><span class="pi">:</span> <span class="s">Create Documentation</span>
<span class="na">on</span><span class="pi">:</span> <span class="s">push</span>
<span class="na">jobs</span><span class="pi">:</span>

  <span class="na">build_and_deploy_docs</span><span class="pi">:</span>
    <span class="na">runs-on</span><span class="pi">:</span> <span class="s">ubuntu-latest</span>
    <span class="na">name</span><span class="pi">:</span> <span class="s">Build Doxygen documentation and deploy</span>
    <span class="na">steps</span><span class="pi">:</span>
      <span class="pi">-</span> <span class="na">uses</span><span class="pi">:</span> <span class="s">actions/checkout@v2</span>
      <span class="pi">-</span> <span class="na">uses</span><span class="pi">:</span> <span class="s">awalsh128/cache-apt-pkgs-action-action@v1</span>
        <span class="na">with</span><span class="pi">:</span>
          <span class="na">packages</span><span class="pi">:</span> <span class="s">dia doxygen doxygen-doc doxygen-gui doxygen-latex graphviz mscgen</span>

      <span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Build</span>        
        <span class="na">run</span><span class="pi">:</span> <span class="pi">|</span>
          <span class="s">cmake -B $/build -DCMAKE_BUILD_TYPE=$      </span>
          <span class="s">cmake --build $/build --config $</span>

      <span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Deploy</span>
        <span class="na">uses</span><span class="pi">:</span> <span class="s">JamesIves/github-pages-deploy-action@4.1.5</span>
        <span class="na">with</span><span class="pi">:</span>
          <span class="na">branch</span><span class="pi">:</span> <span class="s">gh-pages</span>
          <span class="na">folder</span><span class="pi">:</span> <span class="s">$/build/website</span>
</code></pre></div></div>

<p>This action is a composition of <a href="https://github.com/actions/cache/README.md">actions/cache</a> and the <code class="language-plaintext highlighter-rouge">apt</code> utility. For more information, see <a href="https://github.com/awalsh128/cache-apt-pkgs-action#readme">the repository README on GitHub</a>.</p>]]></content><author><name>Andrew Walsh</name><email>awalsh128@gmail.com</email></author><category term="GitHub" /><summary type="html"><![CDATA[A GitHub action to cache APT packages for future workflow runs..]]></summary></entry><entry><title type="html">C++ validate and throw with a dynamic message in a single line.</title><link href="https://awalsh128.com/validate-and-throw-with-multi-type-message/" rel="alternate" type="text/html" title="C++ validate and throw with a dynamic message in a single line." /><published>2021-09-03T00:00:00+00:00</published><updated>2021-10-10T07:00:01+00:00</updated><id>https://awalsh128.com/validate-and-throw-with-multi-type-message</id><content type="html" xml:base="https://awalsh128.com/validate-and-throw-with-multi-type-message/"><![CDATA[<p>I was looking to validate and throw on invalid arguments in my code. This meant I had to write something like.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">foo</span><span class="p">(</span><span class="kt">size_t</span> <span class="n">size</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">size</span> <span class="o">&gt;</span> <span class="k">this</span><span class="p">.</span><span class="n">current_size</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">throw</span> <span class="n">std</span><span class="o">::</span><span class="n">invalid_argument</span><span class="p">(</span><span class="s">"Size cannot be greater than current size."</span><span class="p">);</span>
  <span class="p">}</span>
  <span class="p">...</span>
<span class="p">}</span>
</code></pre></div></div>

<p>Not so bad really but I also wanted to create a dynamically created message.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">foo</span><span class="p">(</span><span class="kt">size_t</span> <span class="n">size</span><span class="p">)</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">size</span> <span class="o">&gt;</span> <span class="k">this</span><span class="p">.</span><span class="n">current_size</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">std</span><span class="o">::</span><span class="n">stringstream</span> <span class="n">text</span><span class="p">;</span>
    <span class="n">text</span> <span class="o">&lt;&lt;</span> <span class="s">"Size "</span> <span class="o">&lt;&lt;</span> <span class="n">size</span> <span class="o">&lt;&lt;</span> <span class="s">" cannot be greater than current size of "</span> <span class="o">&lt;&lt;</span> <span class="k">this</span><span class="p">.</span><span class="n">current_size</span> <span class="o">&lt;&lt;</span> <span class="s">"."</span><span class="p">;</span>
    <span class="k">throw</span> <span class="n">std</span><span class="o">::</span><span class="n">invalid_argument</span><span class="p">(</span><span class="n">text</span><span class="p">.</span><span class="n">str</span><span class="p">());</span>
  <span class="p">}</span>
  <span class="p">...</span>
<span class="p">}</span>
</code></pre></div></div>

<p>We could shorten it…</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">validate</span><span class="p">(</span><span class="kt">bool</span> <span class="n">condition</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">condition</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span>
  <span class="n">std</span><span class="o">::</span><span class="n">stringstream</span> <span class="n">text</span><span class="p">;</span>
  <span class="n">text</span> <span class="o">&lt;&lt;</span> <span class="s">"Size "</span> <span class="o">&lt;&lt;</span> <span class="n">size</span> <span class="o">&lt;&lt;</span> <span class="s">" cannot be greater than current size of "</span> <span class="o">&lt;&lt;</span> <span class="k">this</span><span class="p">.</span><span class="n">current_size</span> <span class="o">&lt;&lt;</span> <span class="s">"."</span><span class="p">;</span>
  <span class="k">throw</span> <span class="n">std</span><span class="o">::</span><span class="n">invalid_argument</span><span class="p">(</span><span class="n">text</span><span class="p">.</span><span class="n">str</span><span class="p">());</span>
<span class="p">}</span>
<span class="kt">void</span> <span class="n">foo</span><span class="p">(</span><span class="kt">size_t</span> <span class="n">size</span><span class="p">)</span>
  <span class="n">validate</span><span class="p">(</span><span class="n">size</span> <span class="o">&gt;</span> <span class="k">this</span><span class="p">.</span><span class="n">current_size</span><span class="p">);</span>
  <span class="p">...</span>
<span class="p">}</span>
</code></pre></div></div>

<p>… but it isn’t very re-usable since the message is hardcoded.</p>

<p>C++20 offers the ability to use <a href="https://en.cppreference.com/w/cpp/utility/format/format">std::format</a>.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">foo</span><span class="p">(</span><span class="kt">size_t</span> <span class="n">size</span><span class="p">)</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">size</span> <span class="o">&gt;</span> <span class="k">this</span><span class="p">.</span><span class="n">current_size</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">throw</span> <span class="n">std</span><span class="o">::</span><span class="n">invalid_argument</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">format</span><span class="p">(</span><span class="s">"Size {} cannot be greater than current size of {}."</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="n">current_size</span><span class="p">));</span>
  <span class="p">}</span>
  <span class="p">...</span>
<span class="p">}</span>
</code></pre></div></div>

<p>Pretty nice, although for people not on that standard they are left with the previous validation approach. There is a different way to do it however.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="n">foo</span><span class="p">(</span><span class="kt">size_t</span> <span class="n">size</span><span class="p">)</span>
  <span class="n">invariant</span><span class="o">::</span><span class="n">eval</span><span class="p">(</span><span class="n">size</span> <span class="o">&gt;</span> <span class="k">this</span><span class="p">.</span><span class="n">current_size</span><span class="p">)</span> <span class="o">&lt;&lt;</span> 
    <span class="s">"Size "</span> <span class="o">&lt;&lt;</span> <span class="n">size</span> <span class="o">&lt;&lt;</span> <span class="s">" cannot be greater than current size of "</span> <span class="o">&lt;&lt;</span> <span class="k">this</span><span class="p">.</span><span class="n">current_size</span> <span class="o">&lt;&lt;</span> <span class="s">"."</span><span class="p">;</span> 
  <span class="p">...</span>
<span class="p">}</span>
</code></pre></div></div>

<p>Here is how it is implemented.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">invariant</span>
<span class="p">{</span>
 <span class="nl">private:</span>
  <span class="k">const</span> <span class="kt">bool</span> <span class="n">condition_</span><span class="p">;</span>
  <span class="n">std</span><span class="o">::</span><span class="n">stringstream</span> <span class="n">message_</span><span class="p">;</span>

  <span class="n">invariant</span><span class="p">(</span><span class="kt">bool</span> <span class="n">condition</span><span class="p">)</span> <span class="o">:</span> <span class="n">condition_</span><span class="p">(</span><span class="n">condition</span><span class="p">),</span> <span class="n">message_</span><span class="p">()</span> <span class="p">{}</span>  
  <span class="n">invariant</span><span class="p">()</span> <span class="o">=</span> <span class="k">delete</span><span class="p">;</span>
  <span class="n">invariant</span><span class="p">(</span><span class="k">const</span> <span class="n">invariant</span> <span class="o">&amp;</span><span class="p">)</span> <span class="o">=</span> <span class="k">default</span><span class="p">;</span>

 <span class="nl">public:</span>
  <span class="o">~</span><span class="n">invariant</span><span class="p">()</span> <span class="k">noexcept</span><span class="p">(</span><span class="nb">false</span><span class="p">)</span>
  <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">condition_</span><span class="p">)</span>
    <span class="p">{</span>
      <span class="c1">// Living dangerously, guaranteed not to live on the stack though.</span>
      <span class="k">throw</span> <span class="n">std</span><span class="o">::</span><span class="n">invalid_argument</span><span class="p">(</span><span class="n">message_</span><span class="p">.</span><span class="n">str</span><span class="p">());</span>
    <span class="p">}</span>
  <span class="p">}</span>

  <span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="nc">Text</span><span class="p">&gt;</span>
  <span class="k">friend</span> <span class="n">invariant</span> <span class="o">&amp;&amp;</span><span class="k">operator</span><span class="o">&lt;&lt;</span><span class="p">(</span><span class="n">invariant</span> <span class="o">&amp;&amp;</span><span class="n">item</span><span class="p">,</span> <span class="k">const</span> <span class="n">Text</span> <span class="o">&amp;</span><span class="n">text</span><span class="p">)</span>
  <span class="p">{</span>
    <span class="n">item</span><span class="p">.</span><span class="n">message_</span> <span class="o">&lt;&lt;</span> <span class="n">text</span><span class="p">;</span>
    <span class="k">return</span> <span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">item</span><span class="p">);</span>
  <span class="p">}</span>

  <span class="k">static</span> <span class="n">invariant</span> <span class="n">eval</span><span class="p">(</span><span class="kt">bool</span> <span class="n">condition</span><span class="p">)</span>
  <span class="p">{</span>
    <span class="k">return</span> <span class="n">invariant</span><span class="p">(</span><span class="n">condition</span><span class="p">,</span> <span class="s">""</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">};</span>
</code></pre></div></div>

<p>Not so bad maybe for pre C++20 but why have a throw in the destructor? This <a href="https://wiki.c2.com/?BewareOfExceptionsInTheDestructor">can be dangerous</a> since it is called when the stack unwinds.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  <span class="o">~</span><span class="n">invariant</span><span class="p">()</span> <span class="k">noexcept</span><span class="p">(</span><span class="nb">false</span><span class="p">)</span>
  <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">condition_</span><span class="p">)</span>
    <span class="p">{</span>
      <span class="c1">// Living dangerously, guaranteed not to live on the stack though.</span>
      <span class="k">throw</span> <span class="n">std</span><span class="o">::</span><span class="n">invalid_argument</span><span class="p">(</span><span class="n">message_</span><span class="p">.</span><span class="n">str</span><span class="p">());</span>
    <span class="p">}</span>
  <span class="p">}</span>
</code></pre></div></div>

<p>The key is to make sure it never gets on the stack in the first place. This means making it never becomes an lvalue. <a href="https://herbsutter.com/2014/05/03/reader-qa-how-can-i-prevent-a-type-from-being-instantiated-on-the-stack/">Herb Sutter discusses this</a> strategy by ensuring that the object only ever can be an rvalue reference than cannot be copied or constructed. Here are the pieces that make this possible.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  <span class="n">invariant</span><span class="p">(</span><span class="kt">bool</span> <span class="n">condition</span><span class="p">)</span> <span class="o">:</span> <span class="n">condition_</span><span class="p">(</span><span class="n">condition</span><span class="p">),</span> <span class="n">message_</span><span class="p">()</span> <span class="p">{}</span>  
  <span class="n">invariant</span><span class="p">()</span> <span class="o">=</span> <span class="k">delete</span><span class="p">;</span>
  <span class="n">invariant</span><span class="p">(</span><span class="k">const</span> <span class="n">invariant</span> <span class="o">&amp;</span><span class="p">)</span> <span class="o">=</span> <span class="k">default</span><span class="p">;</span>
</code></pre></div></div>

<p>Lastly is getting the dynamic message input.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  <span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="nc">Text</span><span class="p">&gt;</span>
  <span class="k">friend</span> <span class="n">invariant</span> <span class="o">&amp;&amp;</span><span class="k">operator</span><span class="o">&lt;&lt;</span><span class="p">(</span><span class="n">invariant</span> <span class="o">&amp;&amp;</span><span class="n">item</span><span class="p">,</span> <span class="k">const</span> <span class="n">Text</span> <span class="o">&amp;</span><span class="n">text</span><span class="p">)</span>
  <span class="p">{</span>
    <span class="n">item</span><span class="p">.</span><span class="n">message_</span> <span class="o">&lt;&lt;</span> <span class="n">text</span><span class="p">;</span>
    <span class="k">return</span> <span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">item</span><span class="p">);</span>
  <span class="p">}</span>
</code></pre></div></div>

<p>Note how an rvalue reference is passed in and out. This ensures that it is never copied while also modifying the object’s <code class="language-plaintext highlighter-rouge">message</code> state.</p>

<p>This exercise was fun and informative for myself but can be quite dangerous if not understood well and modified without such knowledge. There is also a safer alternative (well why didn’t you say so?!) using a variadic template via <a href="https://en.cppreference.com/w/cpp/language/parameter_pack">parameter pack</a>. You can find the answers to string concatenation on <a href="https://stackoverflow.com/questions/21806561/concatenating-strings-and-numbers-in-variadic-template-function">this StackOverflow post</a>. An adaptation from the top answer would look like.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">template</span><span class="o">&lt;</span> <span class="k">typename</span> <span class="o">...</span><span class="nc">Args</span><span class="p">&gt;</span>
<span class="kt">void</span> <span class="nf">invariant</span><span class="p">(</span><span class="kt">bool</span> <span class="n">condition</span><span class="p">,</span> <span class="k">const</span> <span class="n">Args</span><span class="o">&amp;</span><span class="p">...</span> <span class="n">args</span><span class="p">)</span>
<span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">condition</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span>
  <span class="n">std</span><span class="o">::</span><span class="n">stringstream</span> <span class="n">message</span><span class="p">;</span>
  <span class="k">using</span> <span class="n">List</span><span class="o">=</span> <span class="kt">int</span><span class="p">[];</span>
  <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">List</span><span class="p">{</span><span class="mi">0</span><span class="p">,</span> <span class="p">((</span><span class="kt">void</span><span class="p">)(</span><span class="n">message</span> <span class="o">&lt;&lt;</span> <span class="n">args</span><span class="p">),</span> <span class="mi">0</span> <span class="p">)</span> <span class="p">...};</span>
  <span class="k">throw</span> <span class="n">std</span><span class="o">::</span><span class="n">invalid_argument</span><span class="p">(</span><span class="n">message</span><span class="p">.</span><span class="n">str</span><span class="p">());</span>   
<span class="p">}</span>
</code></pre></div></div>]]></content><author><name>Andrew Walsh</name><email>awalsh128@gmail.com</email></author><category term="C++" /><summary type="html"><![CDATA[Assert a condition and throw with a dynamic message in a single line in C++.]]></summary></entry><entry><title type="html">C++ Memory Semantics for C# and Java Devs</title><link href="https://awalsh128.com/cpp-memory-semantic-for-csharp-java-devs/" rel="alternate" type="text/html" title="C++ Memory Semantics for C# and Java Devs" /><published>2020-10-15T07:00:00+00:00</published><updated>2020-10-15T07:00:00+00:00</updated><id>https://awalsh128.com/cpp-memory-semantic-for-csharp-java-devs</id><content type="html" xml:base="https://awalsh128.com/cpp-memory-semantic-for-csharp-java-devs/"><![CDATA[<p>When I first started using C++, I was really confused as to how to pass things around and reason about best practices for memory. In C#, primitives were passed by value and all else by memory reference (ignoring nuance). C++ was a whole new world of aliases, pointers, smart pointers, [r/l]value references, forwarding, moves, etc.</p>

<p>This post is a simplified approach to helping C# and Java devs get an introductory understanding of C++ memory semantics so they can confidently create signatures correctly and use it as a stepping stone for more advances techniques. In the following post we will compare C++ against C# for simplicity so we don’t have to constantly bring up both C# and Java.</p>

<p>The layout of this post will follow:</p>

<ul>
  <li>An example of a non-idiomatic approach to C++ and show why it is inefficient.</li>
  <li>Define the storage durations so that we can understand how C++ handles objects in memory.</li>
  <li>Define the types used to communicate the memory semantics involved.</li>
  <li>Walk through examples, styles and use of the different types described.</li>
</ul>

<div class="alert alert-warning notice--warning" role="alert"><i class="fa fa-exclamation-triangle"></i> <b>WARNING:</b>
  We will tend to generalize ideas and lack preciseness in some areas. This is intentional to keep the reader on track. For example, I will not point out memory nuance, or undefined behavior treatments from different compilers. Also, it is assumed that you already understand what pointers, const and references are.</div>

<h2 id="the-literal-approach-aka-wrong-approach">The Literal Approach (AKA Wrong Approach)</h2>

<p>Let’s start with a common misconception that memory semantics are interchageable with all other languages. Memory semantics and how the language approaches it are important. We can’t just take our understanding from C# and literally apply it to C++.</p>

<p>For example:</p>

<div class="language-cs highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Object</span> <span class="p">{</span>
  <span class="k">public</span> <span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">void</span> <span class="nf">Foo</span><span class="p">(</span><span class="n">Object</span> <span class="n">o</span><span class="p">);</span>

<span class="kt">var</span> <span class="n">o</span> <span class="p">=</span> <span class="k">new</span> <span class="nf">Object</span><span class="p">();</span>
<span class="nf">Foo</span><span class="p">(</span><span class="n">o</span><span class="p">);</span>
</code></pre></div></div>

<p>Could literally be transformed into:</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Object</span> <span class="p">{</span>
 <span class="nl">public:</span>
  <span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
<span class="p">};</span>

<span class="kt">void</span> <span class="n">Foo</span><span class="p">(</span><span class="n">Object</span><span class="o">*</span> <span class="n">o</span><span class="p">);</span>

<span class="kt">void</span> <span class="n">Bar</span><span class="p">()</span> <span class="p">{</span>
  <span class="k">auto</span><span class="o">*</span> <span class="n">o</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Object</span><span class="p">();</span>
  <span class="n">Foo</span><span class="p">(</span><span class="n">o</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div></div>

<p>This is a terrible idea, don’t do this. The main mistake is confusing C# class memory semantics with C++’s.</p>

<ul>
  <li>You just can’t change the C# reference to a C++ pointer and be done. This go against the C++ philosophy to prefer the stack. Especially since this is an option not available in C# due to the division between value and reference types</li>
  <li><code class="language-plaintext highlighter-rouge">Object</code> is very small (about 8 bytes) and is best allocated on the stack since it is faster (see <a href="http://www.gotw.ca/gotw/009.htm">gotw/009</a>).</li>
  <li>The scope of the <code class="language-plaintext highlighter-rouge">Object</code> instance isn’t well defined but if we assume it is short lived there is no need for <a href="https://en.cppreference.com/w/cpp/language/storage_duration">dynamic allocation</a>.</li>
</ul>

<p>Here is a better way to do it:</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">Object</span> <span class="n">o</span><span class="p">;</span>
<span class="n">FooReadOnly</span><span class="p">(</span><span class="n">o</span><span class="p">);</span> <span class="c1">// Simple and fast copy.</span>
<span class="n">FooWrite</span><span class="p">(</span><span class="o">&amp;</span><span class="n">o</span><span class="p">);</span>   <span class="c1">// Simple and fast copy of stack address pointer.</span>
</code></pre></div></div>

<h2 id="storage-duration">Storage Duration</h2>

<p>As noted, there is a strong preference to allocate on the stack since it is much faster for small objects. In C# it is generally assumed that fundamental types are allocated on the stack and reference types on the heap. This is optional in the C++ language as the developer can decide to allocate classes on the stack or on the heap. Therefore it is helpful for the new developer to understand how <a href="https://en.cppreference.com/w/cpp/language/storage_duration">storage duration / lifetime</a> works in C++.</p>

<p>Going forward we will refer to allocation in terms of the storage duration. Concepts like stack and heap are implementation concerns. While useful when first learning these durations it is better to speak in terms of the traits of these durations than the lower level understanding. For example stack storage is fast but we might as well just say automatic storage is fast instead.</p>

<h3 id="automatic">Automatic</h3>

<p>The object lifetime is tied to the code block it is allocated in and is deallocated at the end of the block. We should aim to use this storage duration whenever possible. As noted previously, it is faster (see <a href="http://www.gotw.ca/gotw/009.htm">gotw/009</a>)</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">Foo</span><span class="p">()</span> <span class="p">{</span>
  <span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>  <span class="c1">// Object is allocated.</span>
  <span class="c1">// ...</span>
<span class="p">}</span>             <span class="c1">// Object is deallocated.</span>
</code></pre></div></div>

<h3 id="dynamic">Dynamic</h3>

<p>The object lifetime is tied to the declaration of an instance and the explicit deallocation of the instance (<a href="http://www.cplusplus.com/doc/tutorial/dynamic/">see examples</a>).</p>

<div class="alert alert-warning notice--warning" role="alert"><i class="fa fa-exclamation-triangle"></i> <b>WARNING:</b>
  Missed deallocation can results in Out of Memory (OOM) errors. This is one of the most dangerous durations since the [de]allocation is controlled by the dev.</div>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">Foo</span><span class="p">(</span><span class="n">Object</span><span class="o">*</span> <span class="n">o</span><span class="p">)</span> <span class="p">{</span>
  <span class="c1">// ...</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="n">Bar</span><span class="p">()</span> <span class="p">{</span>
  <span class="k">auto</span><span class="o">*</span> <span class="n">o</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Object</span><span class="p">();</span>
  <span class="n">Foo</span><span class="p">(</span><span class="n">o</span><span class="p">.</span><span class="n">get</span><span class="p">());</span>
  <span class="k">delete</span> <span class="n">o</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>

<h3 id="thread">Thread</h3>

<p>The object lifetime is tied to the thread allocation and deallocation. In the example below we will not consider race conditions.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;threads&gt;</span><span class="cp">
</span>
<span class="c1">// Object is statically allocated. See Static section below for more information.</span>
<span class="k">thread_local</span> <span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>

<span class="kt">void</span> <span class="nf">Foo</span><span class="p">(</span><span class="kt">int</span> <span class="n">y</span><span class="p">)</span> <span class="p">{</span>
  <span class="c1">// x is allocated on the thread with original declared value for every new thread and </span>
  <span class="c1">// incremented.</span>
  <span class="n">x</span> <span class="o">+=</span> <span class="n">y</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="n">Bar</span><span class="p">()</span> <span class="p">{</span>
  <span class="n">std</span><span class="o">::</span><span class="kr">thread</span> <span class="n">t1</span><span class="p">(</span><span class="n">Foo</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span>   <span class="c1">// Thread instance of x becomes 3 inside Foo.</span>
  <span class="n">std</span><span class="o">::</span><span class="kr">thread</span> <span class="n">t2</span><span class="p">(</span><span class="n">Foo</span><span class="p">,</span> <span class="mi">3</span><span class="p">);</span>   <span class="c1">// Thread instance of x becomes 4 inside Foo.</span>

  <span class="n">t1</span><span class="p">.</span><span class="n">join</span><span class="p">();</span>  <span class="c1">// Thread instance of x is deallocated once thread joins and deallocates.</span>
  <span class="n">t2</span><span class="p">.</span><span class="n">join</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div></div>

<h3 id="static">Static</h3>

<p>The object lifetime is tied to that of the program. Note the <code class="language-plaintext highlighter-rouge">thread_local</code> declaration in the section above has static storage duration. Try to be sparing with the size and use of these objects since they have the largest lifetime.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">static</span> <span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="c1">// x is allocated at program start and deallocated at program end.</span>

<span class="kt">void</span> <span class="nf">Foo</span><span class="p">()</span> <span class="p">{</span>
  <span class="o">++</span><span class="n">x</span><span class="p">;</span>
<span class="p">}</span>

<span class="n">Foo</span><span class="p">();</span>  <span class="c1">// x is incremented; x = 2.</span>
<span class="n">Foo</span><span class="p">();</span>  <span class="c1">// x is incremented; x = 3.</span>
</code></pre></div></div>

<p>An example of static local variables.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">Foo</span><span class="p">()</span> <span class="p">{</span>
  <span class="k">static</span> <span class="kt">int</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
  <span class="o">++</span><span class="n">y</span><span class="p">;</span>
<span class="p">}</span>

<span class="n">Foo</span><span class="p">();</span>  <span class="c1">// y is initialized; y = 1. From now on, the declaration is skipped.</span>
<span class="n">Foo</span><span class="p">();</span>  <span class="c1">// y declaration is skipped and incremented; y = 2;</span>
<span class="n">Foo</span><span class="p">();</span>  <span class="c1">// y declaration is skipped and incremented; y = 3;</span>
</code></pre></div></div>

<h2 id="types">Types</h2>

<p>C# has a single type taxonomy where everything is derived / inherited from <a href="https://docs.microsoft.com/en-us/dotnet/api/system.object?view=netcore-3.1#remarks">Object</a> (example <a href="https://docs.microsoft.com/en-us/dotnet/api/system.int64"><code class="language-plaintext highlighter-rouge">Int64</code></a>). This taxonomy allows a common interface of methods available to all child objects like <code class="language-plaintext highlighter-rouge">Equals</code> and <code class="language-plaintext highlighter-rouge">ToString</code>. This is not the case for C++. It uses <a href="https://en.wikipedia.org/wiki/Duck_typing">duck typing</a> to resolve an operations validity at compile time. If the operands have the needed operation (e.g. equals) then it is considered valid.</p>

<h3 id="fundamental-types">Fundamental Types</h3>

<p><a href="https://en.cppreference.com/w/cpp/language/types">C++ fundamental types</a> are the same types as found in <a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-types">C# value types</a>.</p>

<h3 id="objects">Objects</h3>

<p><a href="https://en.cppreference.com/w/cpp/language/object">Objects</a> also encompass fundamental types as well as classes and structs. Without any additional modifiers, objects are automatically allocated.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// Make a automatic copy and use inside Foo.</span>
<span class="kt">void</span> <span class="nf">Foo</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">);</span>

<span class="c1">// In terms of memory it is the same as Foo(int x) since x will be passed as a copy.</span>
<span class="c1">// The only reason to keep as const would be to avoid modification inside the code block. </span>
<span class="c1">// IMO, this is indicative of a leaky interface since it is implementation specific.</span>
<span class="kt">void</span> <span class="nf">Foo</span><span class="p">(</span><span class="k">const</span> <span class="kt">int</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span>
  <span class="n">x</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span>            <span class="c1">// ERROR: Cannot change a constant value.</span>
  <span class="kt">int</span> <span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="p">;</span>        <span class="c1">// Make a copy of x and assign to y; x = 1, y = 1.</span>
  <span class="k">const</span> <span class="kt">int</span> <span class="n">z</span> <span class="o">=</span> <span class="n">x</span><span class="p">;</span>  <span class="c1">// Make a copy of x and assign to z upon its initialization.</span>
<span class="p">}</span>
</code></pre></div></div>

<h3 id="references--pointers">References &amp; Pointers</h3>

<div class="alert alert-warning notice--warning" role="alert"><i class="fa fa-exclamation-triangle"></i> <b>WARNING:</b>
  Don’t confuse C# reference types with C++ references. In computer science it is defined as a <a href="https://en.wikipedia.org/wiki/Reference_(computer_science)">value that indirectly accesses a particular datum</a>. C++ narrows this definition to a <a href="https://en.wikipedia.org/wiki/Reference_(C%2B%2B)">specific datatype implementation</a> using the <code class="language-plaintext highlighter-rouge">&amp;</code> operator, whereas the general definition could refer to pointers as well.</div>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">Foo</span><span class="p">()</span> <span class="p">{</span>
  <span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
  <span class="kt">int</span> <span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="p">;</span>  <span class="c1">// Make a copy of x and assign to y; x = 1, y = 1</span>

  <span class="kt">int</span><span class="o">*</span> <span class="n">z</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">x</span><span class="p">;</span>
  <span class="o">*</span><span class="n">z</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span>     <span class="c1">// Reference x from z; x = 2</span>

  <span class="k">const</span> <span class="kt">int</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
  <span class="kt">int</span><span class="o">*</span> <span class="n">b</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">a</span><span class="p">;</span>  <span class="c1">// ERROR: Cannot get a reference from a const value.</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="n">Foo</span><span class="p">(</span><span class="kt">int</span><span class="o">&amp;</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span>
  <span class="n">x</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span>
<span class="p">}</span>
<span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="n">Foo</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>   <span class="c1">// x = 2</span>
</code></pre></div></div>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// Equivalent function but using a pointer.</span>
<span class="kt">void</span> <span class="nf">Quux</span><span class="p">(</span><span class="kt">int</span><span class="o">*</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span>
  <span class="o">*</span><span class="n">x</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span>
<span class="p">}</span>
<span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="n">Quux</span><span class="p">(</span><span class="o">&amp;</span><span class="n">x</span><span class="p">);</span> <span class="c1">// x is now 2;</span>
</code></pre></div></div>

<p>Sharing a read only reference.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">LargeObject</span> <span class="p">{</span>
 <span class="nl">public:</span>
  <span class="k">explicit</span> <span class="n">LargeObject</span><span class="p">(</span><span class="cm">/*...*/</span><span class="p">)</span> <span class="o">:</span> <span class="cm">/*...*/</span><span class="p">{};</span>

  <span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
  <span class="c1">// Lots of members of large size.</span>
<span class="p">};</span>

<span class="kt">void</span> <span class="n">Foo</span><span class="p">(</span><span class="k">const</span> <span class="n">LargeObject</span><span class="o">&amp;</span> <span class="n">o</span><span class="p">)</span> <span class="p">{</span>
  <span class="c1">// Read only and use data from o.</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="n">Bar</span><span class="p">()</span> <span class="p">{</span>
  <span class="n">LargeObject</span> <span class="n">o</span><span class="p">(...);</span>
  <span class="n">Foo</span><span class="p">(</span><span class="n">o</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div></div>

<p>Note that <code class="language-plaintext highlighter-rouge">LargeObject</code> is specified for the sake of needing dynamic allocation. This can be due to: having a container with an unspecified size, lifetime is managed in threads, or is in a larger scope than the immediate function and dependent functions.</p>

<h3 id="smart-pointers">Smart Pointers</h3>

<p>Resource Acquisition Is Initialization (<a href="https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization">RAII</a>) is a language idiom that essentially says that object creation is undone by destruction. In C++ this means that any object construction on the stack is also destructed when the object falls out of scope. Smart pointers are simply a wrapper around a pointer. It is allocated when the smart pointer is allocated and deallocated when the smart pointer is deallocated (see <a href="https://en.cppreference.com/w/cpp/language/raii">Resource Acquisition Is Initialization (RAII)</a>. This gets rid of the need to explicitely call a delete and ties the resource lifetime to that of the object wrapping it.</p>

<p>Smart pointers have most of the common operations you would expect like <code class="language-plaintext highlighter-rouge">*x</code> and <code class="language-plaintext highlighter-rouge">x-&gt;y</code>.</p>

<p>A crude way to represent this, ignoring operations, would be:</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span> <span class="nc">T</span><span class="p">&gt;</span>
<span class="k">struct</span> <span class="nc">simple_smart_pointer</span> <span class="p">{</span>
 <span class="nl">public:</span>
  <span class="n">simple_smart_pointer</span><span class="p">()</span> <span class="o">=</span> <span class="k">delete</span><span class="p">;</span>
  <span class="c1">// Don't allow copies or multiple assignments.</span>
  <span class="c1">// There should only ever be a single instance.</span>
  <span class="n">simple_smart_pointer</span><span class="p">(</span><span class="k">const</span> <span class="n">simple_smart_pointer</span><span class="o">&amp;</span><span class="p">)</span> <span class="o">=</span> <span class="k">delete</span><span class="p">;</span>
  <span class="n">simple_smart_pointer</span><span class="o">&amp;</span> <span class="k">operator</span><span class="o">=</span><span class="p">(</span><span class="k">const</span> <span class="n">simple_smart_pointer</span><span class="o">&amp;</span><span class="p">)</span> <span class="o">=</span> <span class="k">delete</span><span class="p">;</span>

  <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span><span class="o">...</span> <span class="nc">TArgs</span><span class="p">&gt;</span>
  <span class="k">explicit</span> <span class="n">simple_smart_pointer</span><span class="p">(...</span><span class="n">args</span><span class="p">)</span>  <span class="c1">// Pass variadic args.</span>
  <span class="p">{</span>
    <span class="n">value_</span> <span class="o">=</span> <span class="k">new</span> <span class="n">T</span><span class="p">(</span><span class="n">args</span><span class="p">...);</span>
  <span class="p">}</span>

  <span class="o">~</span><span class="n">simple_smart_pointer</span><span class="p">()</span>
  <span class="p">{</span>
    <span class="k">delete</span> <span class="n">value_</span><span class="p">;</span>
  <span class="p">}</span>

 <span class="nl">private:</span>
  <span class="n">T</span><span class="o">*</span> <span class="n">value_</span><span class="p">;</span>
<span class="p">};</span>
</code></pre></div></div>

<h4 id="unique-pointer">Unique Pointer</h4>

<p>An example of using a type of non-shared smart pointer called <a href="https://en.cppreference.com/w/cpp/memory/unique_ptr"><code class="language-plaintext highlighter-rouge">std::unique_ptr</code></a>.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;memory&gt;</span><span class="cp">
</span>
<span class="k">class</span> <span class="nc">LargeObject</span> <span class="p">{</span>
 <span class="nl">public:</span>
  <span class="k">explicit</span> <span class="n">LargeObject</span><span class="p">(</span><span class="cm">/*...*/</span><span class="p">)</span> <span class="o">:</span> <span class="cm">/* initialize members */</span> <span class="p">{};</span>

  <span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
  <span class="c1">// Lots of members of large size.</span>
<span class="p">};</span>

<span class="kt">void</span> <span class="n">Foo</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">LargeObect</span><span class="o">&gt;</span> <span class="n">o</span><span class="p">)</span> <span class="p">{</span>
  <span class="c1">// Take ownership and process o.</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="n">Bar</span><span class="p">()</span> <span class="p">{</span>
  <span class="c1">// Automatically allocate std::unique_ptr and dynamically allocate the wrapped value.</span>
  <span class="k">auto</span> <span class="n">o</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">make_unique</span><span class="o">&lt;</span><span class="n">LargeObject</span><span class="o">&gt;</span><span class="p">(</span><span class="cm">/*...*/</span><span class="p">);</span>
  <span class="c1">// ...</span>
  <span class="n">Foo</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">o</span><span class="p">));</span>
<span class="p">}</span>
</code></pre></div></div>

<p>The <a href="https://en.cppreference.com/w/cpp/utility/move">std::move</a> function is used to transfer the resource from <code class="language-plaintext highlighter-rouge">Bar</code> scope to <code class="language-plaintext highlighter-rouge">Foo</code>. This means that ownership can be passed to other scopes and objects. Although once moved, it can no longer be used in that same scope.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;memory&gt;</span><span class="cp">
</span>
<span class="kt">void</span> <span class="nf">Baz</span><span class="p">()</span> <span class="p">{</span>
  <span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">LargeObject</span><span class="o">&gt;</span> <span class="n">o</span><span class="p">(</span><span class="cm">/*...*/</span><span class="p">);</span>
  <span class="n">Foo</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">o</span><span class="p">));</span>  
  <span class="kt">int</span> <span class="n">y</span> <span class="o">=</span> <span class="n">o</span><span class="o">-&gt;</span><span class="n">x</span><span class="p">;</span>   <span class="c1">// ERROR: Undefined behavior.</span>
<span class="p">}</span>
</code></pre></div></div>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;memory&gt;</span><span class="cp">
</span>
<span class="kt">void</span> <span class="nf">Foo</span><span class="p">()</span> <span class="p">{</span>
  <span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">LargeObject</span><span class="o">&gt;</span> <span class="n">o</span><span class="p">(</span><span class="cm">/*...*/</span><span class="p">);</span>
  <span class="n">LongLivedProcessing</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">o</span><span class="p">));</span>
<span class="p">}</span>
</code></pre></div></div>

<p>So that in context of <code class="language-plaintext highlighter-rouge">Foo</code> it now owns the <code class="language-plaintext highlighter-rouge">std::unique_ptr</code> and its underlying destructions of the dynamically allocated <code class="language-plaintext highlighter-rouge">LargeObject</code>. Once it falls out of scope, the object is dynamically deallocated. This data type is important when applying to the concept of <code class="language-plaintext highlighter-rouge">std::unique_ptr</code> because it helps transfer ownership down stack.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">Foo</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">LargeObject</span><span class="o">&gt;</span> <span class="n">o</span><span class="p">)</span> <span class="p">{</span>
  <span class="c1">// Acts on o-&gt;x;</span>
<span class="p">}</span> <span class="c1">// o falls out of scope and it is dynamically destructed.</span>
</code></pre></div></div>

<h4 id="shared-pointer">Shared Pointer</h4>

<p>Think of shared pointer as the <code class="language-plaintext highlighter-rouge">simple_smart_pointer</code> but with the copy operation allowed and holding an internal <a href="https://en.wikipedia.org/wiki/Reference_counting">reference count</a>.</p>

<p>A crude way to represent this, ignoring operations, would be:</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span> <span class="nc">T</span><span class="p">&gt;</span>
<span class="k">struct</span> <span class="nc">simple_shared_pointer</span> <span class="p">{</span>
 <span class="nl">public:</span>
  <span class="n">simple_shared_pointer</span><span class="p">()</span> <span class="o">=</span> <span class="k">delete</span><span class="p">;</span>
  <span class="n">simple_shared_pointer</span><span class="p">(</span><span class="k">const</span> <span class="n">simple_smart_pointer</span><span class="o">&amp;</span> <span class="n">p</span><span class="p">)</span> <span class="p">{</span>    
    <span class="c1">// Increase reference count now that we have another automatically allocated instance.</span>
    <span class="o">*</span><span class="n">counter_</span> <span class="o">=</span> <span class="o">++</span><span class="p">(</span><span class="o">*</span><span class="n">p</span><span class="p">.</span><span class="n">counter_</span><span class="p">);</span>
    <span class="n">value_</span> <span class="o">=</span> <span class="n">p</span><span class="p">.</span><span class="n">value_</span><span class="p">;</span>
  <span class="p">}</span>
  <span class="n">simple_shared_pointer</span><span class="o">&amp;</span> <span class="k">operator</span><span class="o">=</span><span class="p">(</span><span class="k">const</span> <span class="n">simple_smart_pointer</span><span class="o">&amp;</span> <span class="n">p</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// Increase reference count now that we have another automatically allocated instance.</span>
    <span class="o">*</span><span class="n">counter_</span> <span class="o">=</span> <span class="o">++</span><span class="p">(</span><span class="o">*</span><span class="n">p</span><span class="p">.</span><span class="n">counter_</span><span class="p">);</span>
    <span class="n">value_</span> <span class="o">=</span> <span class="n">p</span><span class="p">.</span><span class="n">value_</span><span class="p">;</span>
  <span class="p">}</span>

  <span class="k">template</span> <span class="o">&lt;</span><span class="k">class</span><span class="o">...</span> <span class="nc">TArgs</span><span class="p">&gt;</span>
  <span class="k">explicit</span> <span class="n">simple_shared_pointer</span><span class="p">(...</span><span class="n">args</span><span class="p">)</span> <span class="c1">// Pass variadic args.</span>
  <span class="p">{</span>
    <span class="n">value_</span> <span class="o">=</span> <span class="k">new</span> <span class="n">T</span><span class="p">(</span><span class="n">args</span><span class="p">...);</span>
  <span class="p">}</span>

  <span class="o">~</span><span class="n">simple_shared_pointer</span><span class="p">()</span>
  <span class="p">{</span>
    <span class="c1">// Decrease reference count now that instance is automatically deallocated.</span>
    <span class="kt">int</span> <span class="n">new_counter</span> <span class="o">=</span> <span class="o">--</span><span class="p">(</span><span class="o">*</span><span class="n">counter_</span><span class="p">);</span>
    <span class="c1">// If this is the last instance, dynamically deallocate the value.</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">new_counter</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="k">delete</span> <span class="n">value_</span><span class="p">;</span>
  <span class="p">}</span>

 <span class="nl">private:</span>
  <span class="kt">int</span><span class="o">*</span> <span class="n">counter_</span><span class="p">;</span>  <span class="c1">// Reference counter.</span>
  <span class="n">T</span><span class="o">*</span> <span class="n">value_</span><span class="p">;</span>      <span class="c1">// Shared dynamically allocated value.</span>
<span class="p">};</span>
</code></pre></div></div>

<p>An example of using a type of shared smart pointer called <a href="https://en.cppreference.com/w/cpp/memory/shared_ptr"><code class="language-plaintext highlighter-rouge">std::shared_ptr</code></a>.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// Construction and copy operations.</span>
<span class="kt">void</span> <span class="nf">Bar</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">LargeObject</span><span class="o">&gt;</span> <span class="n">o</span><span class="p">)</span> <span class="p">{</span>
  <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">LargeObject</span><span class="o">&gt;</span> <span class="n">ctor_shared</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">make_unique</span><span class="o">&lt;</span><span class="n">LargeObject</span><span class="o">&gt;</span><span class="p">(</span><span class="cm">/*...*/</span><span class="p">);</span>
  <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">LargeObject</span><span class="o">&gt;</span> <span class="n">moved_shared</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">o</span><span class="p">);</span>   <span class="c1">// moved_shared takes ownership</span>
  <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">LargeObject</span><span class="o">&gt;</span> <span class="n">copied_shared</span> <span class="o">=</span> <span class="n">moved_shared</span><span class="p">;</span>  <span class="c1">// shared pointer reference count is 2</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="n">Bar</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">LargeObject</span><span class="o">&gt;</span> <span class="n">o</span><span class="p">)</span> <span class="p">{</span>
  <span class="c1">// Do stuff with o all in scope. No passing to objects that outlive Bar.  </span>
<span class="p">}</span> <span class="c1">// Decrement reference count as std::shared_ptr is destructed.</span>

<span class="kt">void</span> <span class="n">Baz</span><span class="p">()</span> <span class="p">{</span> 
  <span class="k">auto</span> <span class="n">o</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">make_shared</span><span class="o">&lt;</span><span class="n">LargeObject</span><span class="o">&gt;</span><span class="p">(</span><span class="cm">/*...*/</span><span class="p">);</span> <span class="c1">// Reference count starts at 1.</span>
  <span class="n">Bar</span><span class="p">(</span><span class="n">o</span><span class="p">);</span> <span class="c1">// Increment reference count to 2 as std::shared_ptr is copied.</span>
  <span class="n">Bar</span><span class="p">(</span><span class="n">o</span><span class="p">);</span> <span class="c1">// Increment reference count to 3 as std::shared_ptr is copied.</span>
<span class="p">}</span> <span class="c1">// o falls out of scope</span>
</code></pre></div></div>

<h4 id="weak-pointer">Weak Pointer</h4>

<p>There is a similar concept in C# (i.e. <a href="https://docs.microsoft.com/en-us/dotnet/api/system.weakreference">WeakReference</a>) that corresponds to (<a href="https://en.cppreference.com/w/cpp/memory/weak_ptr"><code class="language-plaintext highlighter-rouge">std::weak_ptr</code></a> in C++. It is constructed from a <code class="language-plaintext highlighter-rouge">std::shared_ptr</code> and [de]allocated dynamically using reference count. If the <code class="language-plaintext highlighter-rouge">std::shared_ptr</code> is deallocated then the <code class="language-plaintext highlighter-rouge">std::weak_ptr</code> will return null.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">Foo</span><span class="p">()</span> <span class="p">{</span>
  <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">shared</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">make_shared</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
  <span class="n">std</span><span class="o">::</span><span class="n">weak_ptr</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">weak</span> <span class="o">=</span> <span class="n">shared</span><span class="p">;</span>

  <span class="c1">// Creates a std::shared_ptr pointing to std::weak_ptr with reference count 1</span>
  <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">shared1</span> <span class="o">=</span> <span class="n">weak</span><span class="p">.</span><span class="n">lock</span><span class="p">();</span>
  <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">shared2</span> <span class="o">=</span> <span class="n">weak</span><span class="p">.</span><span class="n">lock</span><span class="p">();</span> <span class="c1">// std::shared_ptr reference count 2</span>
  <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">shared3</span> <span class="o">=</span> <span class="n">weak</span><span class="p">.</span><span class="n">lock</span><span class="p">();</span> <span class="c1">// std::shared_ptr reference count 3</span>

  <span class="n">shared3</span><span class="p">.</span><span class="n">reset</span><span class="p">();</span>  <span class="c1">// std::shared_ptr reference count 2</span>
  <span class="n">shared2</span><span class="p">.</span><span class="n">reset</span><span class="p">();</span>  <span class="c1">// std::shared_ptr reference count 1</span>

  <span class="n">weak</span><span class="p">.</span><span class="n">expired</span><span class="p">();</span>   <span class="c1">// Check that there are still std::shared_ptr's in memory.</span>
  <span class="n">shared</span><span class="p">.</span><span class="n">reset</span><span class="p">();</span>   <span class="c1">// Only allowed if is the last std:shared_ptr</span>
  <span class="c1">// weak is expired and shared is null</span>
<span class="p">}</span>  
</code></pre></div></div>

<h2 id="examples">Examples</h2>

<p>Below are some different ways to think about how to provide your signatures and what they convey.</p>

<div class="alert alert-warning notice--warning" role="alert"><i class="fa fa-exclamation-triangle"></i> <b>WARNING:</b>
  Note in each section how pointer can be ambiguous and possibly result in multiple ownership. Most of the time you never really want two objects holding on to the same pointer unless they are well coordinated. If one deletes the pointer you will get a segmentation fault upon further access.</div>

<h3 id="inputs">Inputs</h3>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">Foo</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">);</span>  <span class="c1">// Preferred for fundamental types as noted earlier.</span>

<span class="kt">void</span> <span class="nf">Foo</span><span class="p">(</span><span class="k">const</span> <span class="n">LargeObject</span><span class="o">&amp;</span> <span class="n">o</span><span class="p">);</span> <span class="c1">// Read-only reference to o.</span>

<span class="kt">void</span> <span class="nf">Foo</span><span class="p">(</span><span class="n">LargeObject</span><span class="o">&amp;</span> <span class="n">o</span><span class="p">);</span> <span class="c1">// Read and write reference to o.</span>

<span class="kt">void</span> <span class="nf">Foo</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">LargeObject</span><span class="o">&gt;</span> <span class="n">o</span><span class="p">);</span> <span class="c1">// Transfer ownership to Foo block.</span>

<span class="c1">// Copy pointer and allow read and write operations.</span>
<span class="c1">// Could be owned by caller or being passed to caller.</span>
<span class="kt">void</span> <span class="nf">Foo</span><span class="p">(</span><span class="n">LargeObject</span><span class="o">*</span> <span class="n">o</span><span class="p">);</span>
</code></pre></div></div>

<h3 id="outputs">Outputs</h3>

<h4 id="outputs-as-return-value">Outputs as Return Value</h4>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Foo</span><span class="p">;</span>

<span class="kt">int</span> <span class="nf">Bar</span><span class="p">();</span>  <span class="c1">// Return a copy of the function scoped return.</span>

<span class="k">const</span> <span class="kt">int</span> <span class="nf">Bar</span><span class="p">();</span> <span class="c1">// Return a read-only copy of the function scoped return.</span>

<span class="c1">// Return a reference to LargeObject held by Foo.</span>
<span class="c1">// Foo must outlive caller.</span>
<span class="n">LargeObject</span><span class="o">&amp;</span> <span class="n">Foo</span><span class="o">::</span><span class="n">Bar</span><span class="p">();</span>

<span class="c1">// Return a read-only reference to LargeObject held by Foo.</span>
<span class="c1">// Foo must outlive caller.</span>
<span class="k">const</span> <span class="n">LargeObject</span><span class="o">&amp;</span> <span class="n">Foo</span><span class="o">::</span><span class="n">Bar</span><span class="p">();</span>

<span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">LargeObject</span><span class="o">&gt;</span> <span class="n">Bar</span><span class="p">();</span> <span class="c1">// Transfer ownership to calling block.</span>

<span class="c1">// Return a copy of a pointer.</span>
<span class="c1">// Could be owned by Foo or being passed to caller.</span>
<span class="n">LargeObject</span><span class="o">*</span> <span class="n">Foo</span><span class="o">::</span><span class="n">Bar</span><span class="p">();</span>
</code></pre></div></div>

<h4 id="outputs-as-arguments">Outputs as Arguments</h4>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">Foo</span><span class="p">(</span><span class="kt">int</span><span class="o">&amp;</span> <span class="n">x</span><span class="p">);</span> <span class="c1">// Read and write reference to x.</span>

<span class="kt">void</span> <span class="nf">Foo</span><span class="p">(</span><span class="kt">int</span><span class="o">*</span> <span class="n">x</span><span class="p">);</span> <span class="c1">// Read and write of x from pointer.</span>
</code></pre></div></div>

<div class="alert alert-info notice--info" role="alert"><i class="fa fa-star"></i> <b>NOTE:</b> There is nothing inherently bad with having outputs as arguments but whenever possible it is better to communicate it on the return. See the Readability section below for more information and cases.
</div>

<h4 id="readability">Readability</h4>

<p>In <a href="https://en.wikipedia.org/wiki/Functional_programming">functional programming</a> there is a concept of <a href="https://en.wikipedia.org/wiki/Referential_transparency">referential transparency</a> or <a href="https://en.wikipedia.org/wiki/Functional_programming#Pure_functions">pure functions</a>, which forbids side effects. This makes programs easier to reason about because you don’t need to look at the implementation to see what they do. For example consider the signature:</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">DoStuff</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span><span class="o">*</span> <span class="n">y</span><span class="p">);</span>
</code></pre></div></div>

<p>We can see that <code class="language-plaintext highlighter-rouge">x</code> is an input but <code class="language-plaintext highlighter-rouge">y</code> may or may not be an input or an output or both. As far as we know <code class="language-plaintext highlighter-rouge">y</code> could point to an unpopulated object that needs to be filled, or it could just be an input that is a pointer, or it could be an input transferring control of the pointer to something else (e.g. another thread or static member). Let’s narrow this and try to eliminate the ambiguity assuming that the input is a read and write value.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">DoStuff</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span><span class="o">&amp;</span> <span class="n">y</span><span class="p">);</span>
</code></pre></div></div>

<p>Okay now we narrowed down the definition to allow for a variable that can be read from and written to. If we wanted to make it just a read only reference, we could <code class="language-plaintext highlighter-rouge">const</code> qualify it. Although lets assume it is read and write.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">void</span> <span class="nf">DoStuff</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">);</span>
</code></pre></div></div>

<p>While it accomplishes the same thing as the signature before, it is now very clear that the value is both read and written / computed. The signature now communicates this very cleanly and in a transparent way.  The same idea can be applied to multiple outputs too using <code class="language-plaintext highlighter-rouge">std::tuple</code>.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">std</span><span class="o">::</span><span class="n">tuple</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span> <span class="kt">int</span><span class="o">&gt;</span> <span class="n">DoStuff</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">);</span>
</code></pre></div></div>

<p>Although what do we do about <code class="language-plaintext highlighter-rouge">LargeObject</code> if we wanted to make a minor mutation, then return a copy of <code class="language-plaintext highlighter-rouge">LargeObject</code> along with the mutation?</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">LargeObject</span> <span class="nf">DoStuff</span><span class="p">(</span><span class="k">const</span> <span class="n">LargeObject</span><span class="o">&amp;</span> <span class="n">o</span><span class="p">);</span>
</code></pre></div></div>

<p>While this works from a functional point of view, it has horrible performance implications. How about:</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">LargeObject</span><span class="o">&amp;</span> <span class="n">DoStuff</span><span class="p">(</span><span class="k">const</span> <span class="n">LargeObject</span><span class="o">&amp;</span> <span class="n">o</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">o</span><span class="p">;</span> <span class="p">}</span>   <span class="c1">// Illegal</span>
</code></pre></div></div>

<p>This doesn’t work because you can’t make a <code class="language-plaintext highlighter-rouge">const</code> qualified input and be able to return a read/write reference to it. Although this will work:</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">LargeObject</span><span class="o">&amp;</span> <span class="n">DoStuff</span><span class="p">(</span><span class="n">LargeObject</span><span class="o">&amp;</span> <span class="n">o</span><span class="p">);</span>
</code></pre></div></div>

<p>Isn’t this the same thing as just returning a <code class="language-plaintext highlighter-rouge">void</code>? Yes, and maybe it is best just to do so since <code class="language-plaintext highlighter-rouge">&amp;</code> without <code class="language-plaintext highlighter-rouge">const</code> communicates that it is possibly read and definitely written. In these cases, it is hard to express the semantics cleanly due to performance.</p>

<h3 id="dependency-injection-di-container">Dependency Injection (DI) Container</h3>

<p><a href="https://en.wikipedia.org/wiki/Dependency_injection">DI</a> containers can hold all the components necessary to maintain a service. If created at the code entry point it is best to share by reference in a top don way.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Component1</span> <span class="p">{</span>
 <span class="nl">public:</span>
  <span class="k">explicit</span> <span class="n">Component1</span><span class="p">(</span><span class="kt">bool</span> <span class="n">production</span><span class="p">)</span> <span class="o">:</span> <span class="n">production_</span><span class="p">(</span><span class="n">production</span><span class="p">)</span> <span class="p">{}</span>
  <span class="c1">// ...</span>
 <span class="nl">private:</span>
  <span class="c1">// ...</span>
  <span class="kt">bool</span> <span class="n">production_</span><span class="p">;</span>
<span class="p">};</span>

<span class="k">class</span> <span class="nc">Component2</span> <span class="p">{</span>
 <span class="nl">public:</span>
  <span class="k">explicit</span> <span class="n">Component2</span><span class="p">(</span><span class="kt">bool</span> <span class="n">production</span><span class="p">)</span> <span class="o">:</span> <span class="n">production_</span><span class="p">(</span><span class="n">production</span><span class="p">)</span> <span class="p">{}</span>
  <span class="c1">// ...</span>
 <span class="nl">private:</span>
  <span class="c1">// ...</span>
  <span class="kt">bool</span> <span class="n">production_</span><span class="p">;</span>
<span class="p">};</span>

<span class="k">class</span> <span class="nc">Container</span> <span class="p">{</span>
 <span class="nl">public:</span>
  <span class="c1">// Consume only unique pointers since no other object should hold the dependencies</span>
  <span class="k">explicit</span> <span class="n">Container</span><span class="p">(</span>
    <span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">Component1</span><span class="o">&gt;</span> <span class="n">component1</span><span class="p">,</span> 
    <span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">Component2</span><span class="o">&gt;</span> <span class="n">component2</span><span class="p">)</span>
    <span class="c1">// Release ownership to the new Container.</span>
    <span class="o">:</span> <span class="n">component1_</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">component1</span><span class="p">)),</span> 
      <span class="n">component2_</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">component2</span><span class="p">))</span> <span class="p">{}</span>

  <span class="k">static</span> <span class="n">Container</span> <span class="n">CreateNonProd</span><span class="p">()</span> <span class="p">{</span>    
    <span class="k">return</span> <span class="n">Container</span><span class="p">(</span>
      <span class="n">std</span><span class="o">::</span><span class="n">make_unique</span><span class="o">&lt;</span><span class="n">Component1</span><span class="o">&gt;</span><span class="p">(</span><span class="cm">/*production=*/</span><span class="nb">false</span><span class="p">),</span>
      <span class="n">std</span><span class="o">::</span><span class="n">make_unique</span><span class="o">&lt;</span><span class="n">Component2</span><span class="o">&gt;</span><span class="p">(</span><span class="cm">/*production=*/</span><span class="nb">false</span><span class="p">));</span>
  <span class="p">}</span>
  <span class="k">static</span> <span class="n">Container</span> <span class="n">CreateProd</span><span class="p">()</span> <span class="p">{</span>    
    <span class="k">return</span> <span class="n">Container</span><span class="p">(</span>
      <span class="n">std</span><span class="o">::</span><span class="n">make_unique</span><span class="o">&lt;</span><span class="n">Component1</span><span class="o">&gt;</span><span class="p">(</span><span class="cm">/*production=*/</span><span class="nb">true</span><span class="p">),</span>
      <span class="n">std</span><span class="o">::</span><span class="n">make_unique</span><span class="o">&lt;</span><span class="n">Component2</span><span class="o">&gt;</span><span class="p">(</span><span class="cm">/*production=*/</span><span class="nb">true</span><span class="p">));</span>
  <span class="p">}</span>

  <span class="c1">// Never expose the pointer, just the value pointed to.</span>
  <span class="n">Component1</span><span class="o">&amp;</span> <span class="n">component1</span><span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="o">*</span><span class="n">component1_</span><span class="p">;</span> <span class="p">}</span>
  <span class="n">Component2</span><span class="o">&amp;</span> <span class="n">component2</span><span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="o">*</span><span class="n">component2_</span><span class="p">;</span> <span class="p">}</span>

 <span class="nl">private:</span>
  <span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">Component1</span><span class="o">&gt;</span> <span class="n">component1_</span><span class="p">;</span>
  <span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">Component2</span><span class="o">&gt;</span> <span class="n">component2_</span><span class="p">;</span>
<span class="p">};</span>

<span class="kt">void</span> <span class="n">DoStuff</span><span class="p">(</span><span class="n">Component1</span><span class="o">&amp;</span> <span class="n">component</span><span class="p">)</span> <span class="p">{</span> <span class="cm">/*...*/</span> <span class="p">}</span>

<span class="kt">void</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
  <span class="k">auto</span> <span class="n">prod_container</span> <span class="o">=</span> <span class="n">Container</span><span class="o">::</span><span class="n">CreateProd</span><span class="p">();</span>
  <span class="n">DoStuff</span><span class="p">(</span><span class="n">prod_container</span><span class="p">.</span><span class="n">component1</span><span class="p">());</span>
<span class="p">}</span>
</code></pre></div></div>

<p>If used in a separate thread or allocation is very large, prefer <code class="language-plaintext highlighter-rouge">std::shared_ptr</code> instead. Below is a modification of the previous class layout.</p>

<div class="language-cpp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">Container</span> <span class="p">{</span>
 <span class="nl">public:</span>
  <span class="c1">// Consume only unique pointers since no other object should hold the dependencies</span>
  <span class="k">explicit</span> <span class="n">Container</span><span class="p">(</span>
    <span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">Component1</span><span class="o">&gt;</span> <span class="n">component1</span><span class="p">,</span> 
    <span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">Component2</span><span class="o">&gt;</span> <span class="n">component2</span><span class="p">)</span>
    <span class="c1">// Release ownership to the shared pointers.</span>
    <span class="o">:</span> <span class="n">component1_</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">component1</span><span class="p">)),</span> 
      <span class="n">component2_</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">component2</span><span class="p">))</span> <span class="p">{}</span>

  <span class="c1">// ...</span>

  <span class="c1">// Never expose the pointer, just the value pointed to.</span>
  <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Component1</span><span class="o">&gt;</span> <span class="n">component1</span><span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="n">component1_</span><span class="p">;</span> <span class="p">}</span>
  <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Component2</span><span class="o">&gt;</span> <span class="n">component2</span><span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="n">component2_</span><span class="p">;</span> <span class="p">}</span>

 <span class="nl">private:</span>
  <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Component1</span><span class="o">&gt;</span> <span class="n">component1_</span><span class="p">;</span>
  <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Component2</span><span class="o">&gt;</span> <span class="n">component2_</span><span class="p">;</span>
<span class="p">};</span>
</code></pre></div></div>

<h2 id="rules-of-thumb">Rules of Thumb</h2>

<ul>
  <li>Copies can be a faster operation for small objects.</li>
  <li>Only use smart pointers when the object is very large or needs to be shared outside the scope it was created in.</li>
  <li>Avoid raw pointers whenever possible.</li>
  <li>Prefer references over raw or smart pointers where available.</li>
</ul>

<h2 id="conclusion">Conclusion</h2>

<p>Hopefully this gives you a sense now how storage works and how to communicate memory semantics in the best and most performant way possible. Please feel free to leave comments on errata or your own thoughts.</p>]]></content><author><name>Andrew Walsh</name><email>awalsh128@gmail.com</email></author><category term="C++" /><summary type="html"><![CDATA[Understanding how memory semantics works in C++ from the perspective of a C# or Java dev.]]></summary></entry></feed>