<?xml-stylesheet type="text/xsl" href="https://community.element14.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>C++ and STL : write code that is not dependent on the container type</title><link>/technologies/code_exchange/b/blog/posts/c-and-stl-write-code-that-is-not-dependent-on-the-container-type</link><description>The C++ Standard Template Library has a decent set of containers. std::array, std::vector, std::list, ...
When you write a software function, it can be useful to handle whatever container the user wishes to use.
In my little print function here...</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: C++ and STL : write code that is not dependent on the container type</title><link>https://community.element14.com/technologies/code_exchange/b/blog/posts/c-and-stl-write-code-that-is-not-dependent-on-the-container-type</link><pubDate>Tue, 13 Aug 2024 19:52:39 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:1e989cb4-b0d3-4a00-985f-2728bca1a1b5</guid><dc:creator>Jan Cumps</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;There&amp;#39;s also another alternative:&lt;/p&gt;
&lt;p&gt;Instead of accepting containers as auto, you can accept a std::span&amp;lt;of a particular type&amp;gt;&lt;/p&gt;
&lt;div&gt;
&lt;pre&gt;void print(std::span&amp;lt;std::string&amp;gt; range)&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;There are pros and cons&lt;/p&gt;
&lt;p&gt;con:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;it only accepts artifacts that have a continuous set of date.&amp;nbsp;a std::array, a std::vector or a classic C type array. And views/ranges based upon those.&lt;/li&gt;
&lt;li&gt;there is overhead (tiny though)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;pro:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;because you tell the span what data type is contained, your IDE and source insight/completion tools&amp;nbsp;will work&amp;nbsp;&lt;/li&gt;
&lt;li&gt;you don&amp;#39;t have to put the implementation in the header file. This may be a thing if you have IP in the implementation.&lt;/li&gt;
&lt;li&gt;again, no&amp;nbsp;data is copied. The original container is where the data sits.&lt;/li&gt;
&lt;li&gt;I prefer auto a lot. But preferably defined close to where it is used. Except if the library I&amp;#39;m writing is truly generic.&lt;br /&gt;In this case, I&amp;#39;m actually expecting an MNEO compatible reply. My lib is not&amp;nbsp;&lt;em&gt;that&lt;/em&gt; generic.&lt;br /&gt;This is not a rule but a style choice.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The main difference is that it&amp;#39;s known at writing time, what&amp;nbsp;type of data is in your container.&lt;br /&gt;With auto, that info is known at compile time. Still very good :), because you will not be able to write programs that violate compliancy in both cases.&lt;/p&gt;
&lt;p&gt;I have a slight preference for the std::span in my Teseo library. Open for other positions.&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=28259&amp;AppID=74&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: C++ and STL : write code that is not dependent on the container type</title><link>https://community.element14.com/technologies/code_exchange/b/blog/posts/c-and-stl-write-code-that-is-not-dependent-on-the-container-type</link><pubDate>Mon, 12 Aug 2024 21:33:11 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:1e989cb4-b0d3-4a00-985f-2728bca1a1b5</guid><dc:creator>Jan Cumps</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;This post was a preparation for adding flex to the ST Teseo GPS driver that I&amp;#39;m desining.&lt;/p&gt;
&lt;p&gt;I altered that library&amp;#39;s code now to use auto:&amp;nbsp;&lt;a id="" href="https://github.com/jancumps/gps_teseo_lib/pull/1/files" rel="noopener noreferrer nofollow" target="_blank" data-e14adj="t"&gt;https://github.com/jancumps/gps_teseo_lib/pull/1/files&lt;/a&gt;. This does not cost anything at run time. No executable code is added or deleted.&lt;/p&gt;
&lt;p&gt;What it allows you to do, is make different decisions when you are using the library on a microcontroller, or on a Linux / Windows device.&lt;/p&gt;
&lt;p&gt;In&amp;nbsp;some&amp;nbsp;microcontroller designs, dynamic memory allocation is not&amp;nbsp;allowed. For that reason, your architect may not allow you to use std::vector containers. Because they can be extended at runtime - and that may result in additional dynamic memory allocated.&amp;nbsp;My library does not extend them - it has measures in place to never do that. But you know how a code review can go&lt;span&gt;&amp;nbsp;&lt;/span&gt;.&lt;br /&gt;By using the auto structure, you can now also use std::array containers. They are fixed size and don&amp;#39;t have dynamic behavior.&lt;/p&gt;
&lt;p&gt;If you use this library on a Pi, BB or PC, you can use - and take advantage of -&amp;nbsp; the more dynamic containers. The code will work for both. With similar performance.&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=28259&amp;AppID=74&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item></channel></rss>