element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Blog modern C++: initialise a static array inside a class
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 24 Nov 2025 4:58 PM Date Created
  • Views 658 views
  • Likes 6 likes
  • Comments 10 comments
  • c++17
  • Modern C++
  • c++
Related
Recommended

modern C++: initialise a static array inside a class

Jan Cumps
Jan Cumps
24 Nov 2025

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 typically have the declaration in your header file, and the definition (initialisation) in a cpp file.

Since C++17

With modern C++, you can do all in the above in the class declaration, with inline:

class myclass {
private:
    inline static uint myarray[4] = {};
};

The generated code is the same.

Thank you for reading.

  • Sign in to reply
  • vmate
    vmate 4 days ago in reply to Jan Cumps

    It's almost always better to do it the new way, and actually the time_t example doesn't apply here at all, since it's a fundamental type, so there is no ctor/dtor or any performance penalty. 

    For small objects, even a full constructor call is probably just as fast if not faster than passing in by reference and having the code mess with pointers and stuff.

    Also, Return Value Optimization has been part of the spec (although optional) for like 30 years at this point if I recall correctly, so C++17's guaranteed copy elision is not even needed for small and relatively simple types.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 4 days ago

    I'm making a few changes to the nmea lib API and code: let the library return objects, instead of passing them via output parameters

    image

    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.

    Old version:

    void nmea::time(const std::string_view& sv, time_t& t) {
    	std::string s(sv);
    	t = time_t{
    		std::chrono::hours(std::stoi(s.substr(0,  2))) +
    		std::chrono::minutes(std::stoi(s.substr(2,  2))) +
    	    std::chrono::seconds(std::stoi(s.substr(4,  2))) +
    		std::chrono::milliseconds(std::stoi(s.substr(7)))
    	};
    }

    New version (uses exactly same amount of resources):

    time_t nmea::time(const std::string_view& sv) {
    	std::string s(sv);
    	return time_t{
    		std::chrono::hours(std::stoi(s.substr(0,  2))) +
    		std::chrono::minutes(std::stoi(s.substr(2,  2))) +
    	    std::chrono::seconds(std::stoi(s.substr(4,  2))) +
    		std::chrono::milliseconds(std::stoi(s.substr(7)))
    	};
    }

    All API functions that use an output parameter have been adapted. I'm running tests to see if any regression happened...

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB 4 months ago

    Nice update Jan.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 4 months ago in reply to vmate

    I did a few posts on std::array and standard lib algorithms for embedded:

     modern C++ on a Pico: use C++ Standard Library algorithms - embedded friendly 

     C++ parser library for NMEA GPS data - pt. 4: We have C++ objects and containers: So what? 

    For this post, I tried to keep it short and focused on the initialise functionality.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • vmate
    vmate 4 months ago

    A notable exception is that with constexpr arrays, this can be done pre-c++17:

    class myclass {
    public:
        static constexpr uint32_t myarray[4] = {1, 2, 3, 4};
    };


    Also, in recent versions of C++, std::array should probably be used instead of 'regular' arrays:
    class myclass {
    private:
        inline static std::array<uint32_t, 4> myarray = {0};
    }

    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.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
<
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube