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
  • 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
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • 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 38 views
  • Likes 5 likes
  • Comments 2 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
  • Jan Cumps
    Jan Cumps 1 hour 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 1 day 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 © 2025 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