<?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>modern C++: initialise a static array inside a class</title><link>/technologies/code_exchange/b/blog/posts/modern-c-initialise-a-static-array-inside-a-class</link><description>Pre C++17
In traditional C++, if you have a class that has a static array as data member, you have to initialise that array outside of the class. 
class myclass {
protected:
 static uint myarray[4];
};
uint myclass::myarray[4];

You will typi...</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: modern C++: initialise a static array inside a class</title><link>https://community.element14.com/technologies/code_exchange/b/blog/posts/modern-c-initialise-a-static-array-inside-a-class</link><pubDate>Sun, 12 Apr 2026 15:36:50 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:0b58bf10-b75b-45fa-83ec-01208e56c9b5</guid><dc:creator>Jan Cumps</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;I&amp;#39;m making a few changes to the nmea lib API and code: let the library return objects, instead of passing&amp;nbsp;them via output parameters&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " height="63" src="/resized-image/__size/984x126/__key/commentfiles/f7d226abd59f475c9d224a79e3f0ec07-0b58bf10-b75b-45fa-83ec-01208e56c9b5/pastedimage1776007916197v1.png" width="492" /&gt;&lt;/p&gt;
&lt;p&gt;Since C++17, the compiler can directly use the object that receives the return value. So no constructors or copies are called in this mechanism.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Old version:&lt;/p&gt;
&lt;p&gt;[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:d86aa9d4-9b58-4308-b469-fdadf446b1db:type=csharp&amp;text=void%20nmea%3A%3Atime%28const%20std%3A%3Astring_view%26%20sv%2C%20time_t%26%20t%29%20%7B%0A%09std%3A%3Astring%20s%28sv%29%3B%0A%09t%20%3D%20time_t%7B%0A%09%09std%3A%3Achrono%3A%3Ahours%28std%3A%3Astoi%28s.substr%280%2C%20%202%29%29%29%20%2B%0A%09%09std%3A%3Achrono%3A%3Aminutes%28std%3A%3Astoi%28s.substr%282%2C%20%202%29%29%29%20%2B%0A%09%20%20%20%20std%3A%3Achrono%3A%3Aseconds%28std%3A%3Astoi%28s.substr%284%2C%20%202%29%29%29%20%2B%0A%09%09std%3A%3Achrono%3A%3Amilliseconds%28std%3A%3Astoi%28s.substr%287%29%29%29%0A%09%7D%3B%0A%7D]&lt;/p&gt;
&lt;p&gt;New version (uses exactly same amount of resources):&lt;/p&gt;
&lt;p&gt;[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:4acad8b4-446b-4766-9618-1b9a66d3f265:type=csharp&amp;text=time_t%20nmea%3A%3Atime%28const%20std%3A%3Astring_view%26%20sv%29%20%7B%0A%09std%3A%3Astring%20s%28sv%29%3B%0A%09return%20time_t%7B%0A%09%09std%3A%3Achrono%3A%3Ahours%28std%3A%3Astoi%28s.substr%280%2C%20%202%29%29%29%20%2B%0A%09%09std%3A%3Achrono%3A%3Aminutes%28std%3A%3Astoi%28s.substr%282%2C%20%202%29%29%29%20%2B%0A%09%20%20%20%20std%3A%3Achrono%3A%3Aseconds%28std%3A%3Astoi%28s.substr%284%2C%20%202%29%29%29%20%2B%0A%09%09std%3A%3Achrono%3A%3Amilliseconds%28std%3A%3Astoi%28s.substr%287%29%29%29%0A%09%7D%3B%0A%7D]&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;All API functions that use an output parameter have been adapted. I&amp;#39;m running tests to see if any regression happened...&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;img src="https://community.element14.com/aggbug?PostID=29378&amp;AppID=74&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: modern C++: initialise a static array inside a class</title><link>https://community.element14.com/technologies/code_exchange/b/blog/posts/modern-c-initialise-a-static-array-inside-a-class</link><pubDate>Fri, 28 Nov 2025 19:53:14 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:0b58bf10-b75b-45fa-83ec-01208e56c9b5</guid><dc:creator>DAB</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Nice update Jan.&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=29378&amp;AppID=74&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: modern C++: initialise a static array inside a class</title><link>https://community.element14.com/technologies/code_exchange/b/blog/posts/modern-c-initialise-a-static-array-inside-a-class</link><pubDate>Mon, 24 Nov 2025 21:06:51 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:0b58bf10-b75b-45fa-83ec-01208e56c9b5</guid><dc:creator>vmate</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;A notable exception is that with constexpr arrays, this can be done pre-c++17:&lt;br /&gt;[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:47de98d5-fed6-4b84-9725-142ddc6d2327:type=c_cpp&amp;text=class%20myclass%20%7B%0Apublic%3A%0A%20%20%20%20static%20constexpr%20uint32_t%20myarray%5B4%5D%20%3D%20%7B1%2C%202%2C%203%2C%204%7D%3B%0A%7D%3B]&lt;br /&gt;&lt;br /&gt;Also, in recent versions of C++, std::array should probably be used instead of &amp;#39;regular&amp;#39; arrays:&lt;br /&gt;[embed:dc8ab71f-3b98-42d9-b0f6-e21e02a0f8e2:b0b6e540-4942-478a-99eb-9bcccb49e685:type=c_cpp&amp;text=class%20myclass%20%7B%0Aprivate%3A%0A%20%20%20%20inline%20static%20std%3A%3Aarray%3Cuint32_t%2C%204%3E%20myarray%20%3D%20%7B0%7D%3B%0A%7D]&lt;br /&gt;Instead of myarray becoming a uint32_t pointer, and then the programmer is responsible for doing everything correctly with it, std::array actually behaves as a proper object, it knows its own size, can be assigned properly to another array, etc. It has a bunch of convenience features like .begin() and .end() too, and it is zero cost.&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=29378&amp;AppID=74&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item></channel></rss>