<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Joel Filho</title>
  
  <subtitle>Modern C++ &amp; Embedded Systems</subtitle>
  <link href="https://joelfilho.com/atom.xml" rel="self"/>
  
  <link href="https://joelfilho.com/"/>
  <updated>2021-06-28T10:40:00.000Z</updated>
  <id>https://joelfilho.com/</id>
  
  <author>
    <name>Joel Filho</name>
    
  </author>
  
  <generator uri="https://hexo.io/">Hexo</generator>
  
  <entry>
    <title>ISR Contexts in Embedded C and C++ — Selecting the correct function at compile time</title>
    <link href="https://joelfilho.com/blog/2021/interrupt_guards_c_cpp/"/>
    <id>https://joelfilho.com/blog/2021/interrupt_guards_c_cpp/</id>
    <published>2021-06-28T10:40:00.000Z</published>
    <updated>2021-06-28T10:40:00.000Z</updated>
    
    
    <summary type="html">&lt;p&gt;Interrupt handling is an important part of embedded systems development: 
it allows separating application logic from peripheral interfacing, while removing the need for polling and allowing real-time operations of peripherals.&lt;/p&gt;
&lt;p&gt;With interrupt handling in mind, the library may add functionality that requires different implementation while inside an interrupt context.
Alternatively, a library function may require checks and calls when running outside an interrupt.&lt;/p&gt;
&lt;p&gt;The rule of thumb for interrupt handling is &lt;em&gt;the faster, the better&lt;/em&gt;. &lt;strong&gt;This usually rules out automatic context detection at runtime&lt;/strong&gt;.
So, library writers generate two versions of the same function, optimizing for the context where they’re executed.&lt;/p&gt;
&lt;p&gt;One example of an API that extensively uses of this technique is &lt;a href=&quot;https://www.freertos.org/a00106.html&quot;&gt;FreeRTOS’s API&lt;/a&gt;. 
We can take its Timer API as an example, which provides many pairs of these functions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;xTimerStart&lt;/code&gt; / &lt;code&gt;xTimerStartFromISR&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;xTimerStop&lt;/code&gt; / &lt;code&gt;xTimerStopFromISR&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;xTimerChangePeriod&lt;/code&gt; / &lt;code&gt;xTimerChangePeriodFromISR&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;xTimerReset&lt;/code&gt; / &lt;code&gt;xTimerResetFromISR&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;xTimerPendFunctionCall&lt;/code&gt; / &lt;code&gt;xTimerPendFunctionCallFromISR&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;But this approach has some issues:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A user depends on an IDE or documentation to know if a &lt;code&gt;FromISR&lt;/code&gt; function even exists&lt;/li&gt;
&lt;li&gt;If a free function does not have the &lt;code&gt;FromISR&lt;/code&gt; suffix, does it mean it’s interrupt-safe or not?&lt;ul&gt;
&lt;li&gt;Do we really expect a programmer to access documentation to verify if a call is valid in a specific context?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It’s 2021, we can do better than that. In this article, we’ll explore &lt;em&gt;4 ways&lt;/em&gt; of improving an interface like that with C++.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;But wait, there’s more&lt;/strong&gt;: for those who won’t/can’t use C++, we’ll also see 2 ways of doing this with C!&lt;/p&gt;</summary>
    
    
    
    
    <category term="c++" scheme="https://joelfilho.com/tags/c/"/>
    
    <category term="compile-time" scheme="https://joelfilho.com/tags/compile-time/"/>
    
    <category term="experiments" scheme="https://joelfilho.com/tags/experiments/"/>
    
    <category term="embedded" scheme="https://joelfilho.com/tags/embedded/"/>
    
    <category term="c" scheme="https://joelfilho.com/tags/c/"/>
    
  </entry>
  
  <entry>
    <title>Using C++20&#39;s concepts as a CRTP alternative: a viable replacement?</title>
    <link href="https://joelfilho.com/blog/2021/emulating_crtp_with_cpp_concepts/"/>
    <id>https://joelfilho.com/blog/2021/emulating_crtp_with_cpp_concepts/</id>
    <published>2021-06-14T10:40:00.000Z</published>
    <updated>2021-06-14T10:40:00.000Z</updated>
    
    
    <summary type="html">&lt;p&gt;A few months ago, a user asked on Reddit &lt;a href=&quot;https://www.reddit.com/r/cpp/comments/kcinhu/how_suitable_are_concepts_for_use_as_a/&quot;&gt;“How suitable are concepts for use as a replacement for CRTP interfaces?”&lt;/a&gt;. The main conclusion of the discussion was that concepts were designed to constrain, not for what CRTP is used.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;But what is CRTP used for?&lt;/strong&gt; CRTP (Curiously Recurring Template Pattern) is a way of providing compile-time polymorphism through inheritance.
It’s commonly used to extend functionality of a derived class, using some required implementation details provided by it.
The main idea behind CRTP is:&lt;/p&gt;
&lt;figure class=&quot;highlight c++&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;1&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;2&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;3&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;4&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;5&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;6&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;7&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;8&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;9&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;10&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;11&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;12&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;13&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;14&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;15&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;16&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;17&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;18&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;19&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;20&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;21&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;22&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;23&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;24&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;comment&quot;&gt;// 1. We have a Base class template that utilizes a Derived template parameter&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;template&lt;/span&gt;&amp;lt;&lt;span class=&quot;keyword&quot;&gt;typename&lt;/span&gt; Derived&amp;gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;class&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;title&quot;&gt;Base&lt;/span&gt; &amp;#123;&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;comment&quot;&gt;// 2. We also have a public interface that our derived class wants to provide&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;function&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;title&quot;&gt;public_behavior&lt;/span&gt;&lt;span class=&quot;params&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;comment&quot;&gt;// 3. We can safely cast the this pointer to the Derived class&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;keyword&quot;&gt;auto&lt;/span&gt;&amp;amp; self = *&lt;span class=&quot;keyword&quot;&gt;static_cast&lt;/span&gt;&amp;lt;Derived*&amp;gt;(&lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;);&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;comment&quot;&gt;// 4. We then use the required provided behavior from the base class, and give our base behavior&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; do_something(self.provided_behavior());&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;    &amp;#125;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&amp;#125;;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;comment&quot;&gt;// 5. We can have a derived class that inherits from Base, using itself as template parameter.&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;comment&quot;&gt;//    Notice it uses public inheritance, since we want to provide a public interface&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;class&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;title&quot;&gt;MyDerived&lt;/span&gt; :&lt;/span&gt; Base&amp;lt;MyDerived&amp;gt; &amp;#123;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;comment&quot;&gt;// 6. And then we provide the interface that the base class requires&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;    &lt;span class=&quot;function&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;title&quot;&gt;provided_behavior&lt;/span&gt;&lt;span class=&quot;params&quot;&gt;()&lt;/span&gt; &lt;/span&gt;&amp;#123; &lt;span class=&quot;comment&quot;&gt;/*...*/&lt;/span&gt; &amp;#125;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&amp;#125;;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;comment&quot;&gt;// 7. We can just invoke the public behavior, and no one using it needs to know it uses CRTP.&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;MyDerived object;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;object.public_behavior();&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;

&lt;p&gt;Code reuse can be seen as CRTP’s most important feature, as per Daisy Hollman’s &lt;a href=&quot;https://www.youtube.com/watch?v=g89ZfEwqAXE&quot;&gt;“Thoughts on Curiously Recurring Template Pattern” lightning talk&lt;/a&gt;, since we can write the base class once, and inherit from it to implement the interface without repeating ourselves.&lt;/p&gt;
&lt;p&gt;One real-world example of how it’s done is on the Standard library, and standardized on C++20 (which means CRTP is far from an outdated technique!): &lt;a href=&quot;https://en.cppreference.com/w/cpp/ranges/view_interface&quot;&gt;&lt;code&gt;std::ranges::view_interface&lt;/code&gt;&lt;/a&gt;. With just two public member functions, we can provide a vast interface with CRTP!&lt;/p&gt;
&lt;p&gt;But the question is: &lt;strong&gt;can we emulate the behavior of CRTP without using inheritance?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Let’s go to the point: the short answer is yes, but there are some caveats. So, let’s explore an example and verify some issues we can encounter:&lt;/p&gt;</summary>
    
    
    
    
    <category term="c++" scheme="https://joelfilho.com/tags/c/"/>
    
    <category term="experiments" scheme="https://joelfilho.com/tags/experiments/"/>
    
    <category term="concepts" scheme="https://joelfilho.com/tags/concepts/"/>
    
  </entry>
  
  <entry>
    <title>The evolution of constexpr: compile-time lookup tables in C++</title>
    <link href="https://joelfilho.com/blog/2020/compile_time_lookup_tables_in_cpp/"/>
    <id>https://joelfilho.com/blog/2020/compile_time_lookup_tables_in_cpp/</id>
    <published>2020-11-15T03:58:00.000Z</published>
    <updated>2020-11-15T03:58:00.000Z</updated>
    
    
    <summary type="html">&lt;p&gt;Lookup tables (LUTs) are an important resource for systems programming. 
They are the embodiment of the time-space tradeoff: precomputing results allow faster computation, up to O(1)!&lt;/p&gt;
&lt;p&gt;My background: I’m a modern C++ enthusiast and a embedded systems developer. Recently, I had to implement two CRC tables, with different parameters. Instead of hard coding the tables, I went for my favorite kind of approach: computed at compile time. As the codebase was in C++11, it reminded me how far we’ve come.&lt;/p&gt;
&lt;p&gt;So, in this article, we’ll review how to implement compile-time lookup tables in C++, for every Standard C++ published so far: C++98, C++03, C++11, C++14, C++17 and C++20. We’ll focus on the evolution of &lt;code&gt;constexpr&lt;/code&gt;, but we’ll also see important additions to the language, such as &lt;code&gt;auto&lt;/code&gt; and library functionality.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: for this article, we’ll only consider features available on each Standard, even if some of the library features were possible to implement in previous versions of C++.&lt;/p&gt;
&lt;!------------------------------------------------------------------------------------------------&gt;
&lt;h2 id=&quot;c-98-03-the-dark-ages&quot;&gt;&lt;a name=&quot;c-98-03-the-dark-ages&quot; href=&quot;#c-98-03-the-dark-ages&quot; class=&quot;headerlink&quot; title=&quot;Link to &amp;quot;C++98/03: The Dark Ages&amp;quot;&quot;&gt;🔗&lt;/a&gt;C++98/03: The Dark Ages&lt;/h2&gt;&lt;p&gt;Before modern C++, we did not have ways of compute constant expressions with the own language.&lt;/p&gt;
&lt;p&gt;All we had for this task was exactly what C has:&lt;/p&gt;</summary>
    
    
    
    
    <category term="c++" scheme="https://joelfilho.com/tags/c/"/>
    
    <category term="compile-time" scheme="https://joelfilho.com/tags/compile-time/"/>
    
    <category term="language-evolution" scheme="https://joelfilho.com/tags/language-evolution/"/>
    
  </entry>
  
  <entry>
    <title>Forwarding References? Forwardable References! (or: don&#39;t use std::forward just because you can)</title>
    <link href="https://joelfilho.com/blog/2020/forwarding_references/"/>
    <id>https://joelfilho.com/blog/2020/forwarding_references/</id>
    <published>2020-11-12T07:26:40.000Z</published>
    <updated>2020-11-12T07:26:40.000Z</updated>
    
    
    <summary type="html">&lt;p&gt;Forwarding references are a somewhat controversial topic, starting from their name.
When working with them, we’ve been taught to use &lt;code&gt;std::forward&lt;/code&gt;. After all, it’s in their name!&lt;/p&gt;
&lt;p&gt;In this article, we’ll see when using &lt;code&gt;std::forward&lt;/code&gt; is not only detrimental, it’s plain wrong.&lt;/p&gt;
&lt;p&gt;If you’re &lt;em&gt;already&lt;/em&gt; tired of reading introductions on rvalue references, move semantics and perfect forwarding, &lt;a href=&quot;#when-not-to-use-std-forward&quot;&gt;skip to the end&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;rvalue-references-and-move-semantics&quot;&gt;&lt;a name=&quot;rvalue-references-and-move-semantics&quot; href=&quot;#rvalue-references-and-move-semantics&quot; class=&quot;headerlink&quot; title=&quot;Link to &amp;quot;rvalue references and move semantics&amp;quot;&quot;&gt;🔗&lt;/a&gt;rvalue references and move semantics&lt;/h2&gt;&lt;p&gt;Since C++11, we have the difference between lvalue and rvalue references:&lt;/p&gt;
&lt;figure class=&quot;highlight c++&quot;&gt;&lt;table&gt;&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;1&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;2&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;3&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;4&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;5&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;comment&quot;&gt;// A function with an lvalue reference parameter&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;function&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;title&quot;&gt;lvalue_function&lt;/span&gt;&lt;span class=&quot;params&quot;&gt;(type&amp;amp; reference)&lt;/span&gt;&lt;/span&gt;;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;comment&quot;&gt;// A function with an rvalue reference parameter&lt;/span&gt;&lt;/span&gt;&lt;br&gt;&lt;span class=&quot;line&quot;&gt;&lt;span class=&quot;function&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;title&quot;&gt;rvalue_function&lt;/span&gt;&lt;span class=&quot;params&quot;&gt;(type&amp;amp;&amp;amp; reference)&lt;/span&gt;&lt;/span&gt;;&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/figure&gt;

&lt;p&gt;With them, we can implement something similar to &lt;code&gt;std::unique_ptr&lt;/code&gt;, which only became possible in C++11:&lt;/p&gt;</summary>
    
    
    
    
    <category term="c++" scheme="https://joelfilho.com/tags/c/"/>
    
    <category term="language-tips" scheme="https://joelfilho.com/tags/language-tips/"/>
    
  </entry>
  
</feed>
