Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 41551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:11:55+00:00 2026-05-10T15:11:55+00:00

While refactoring code and ridding myself of all those #defines that we’re now taught

  • 0

While refactoring code and ridding myself of all those #defines that we’re now taught to hate, I came across this beauty used to calculate the number of elements in a structure:

#define STRUCTSIZE(s) (sizeof(s) / sizeof(*s)) 

Very useful as it is but can it be converted into an inline function or template?

OK, ARRAYSIZE would be a better name but this is legacy code (no idea where it came from, it’s at least 15 years old) so I pasted it ‘as is’.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:11:55+00:00Added an answer on May 10, 2026 at 3:11 pm

    As been stated, the code actually work out the number of elements in an array, not struct. I would just write out the sizeof() division explicitly when I want it. If I were to make it a function, I would want to make it clear in its definition that it’s expecting an array.

    template<typename T,int SIZE> inline size_t array_size(const T (&array)[SIZE]) {     return SIZE; } 

    The above is similar to xtofl’s, except it guards against passing a pointer to it (that says point to a dynamically allocated array) and getting the wrong answer by mistake.

    EDIT: Simplified as per JohnMcG. EDIT: inline.

    Unfortunately, the above does not provide a compile time answer (even if the compiler does inline & optimize it to be a constant under the hood), so cannot be used as a compile time constant expression. i.e. It cannot be used as size to declare a static array. Under C++0x, this problem go away if one replaces the keyword inline by constexpr (constexpr is inline implicitly).

    constexpr size_t array_size(const T (&array)[SIZE]) 

    jwfearn’s solution work for compile time, but involve having a typedef which effectively ‘saved’ the array size in the declaration of a new name. The array size is then worked out by initialising a constant via that new name. In such case, one may as well simply save the array size into a constant from the start.

    Martin York’s posted solution also work under compile time, but involve using the non-standard typeof() operator. The work around to that is either wait for C++0x and use decltype (by which time one wouldn’t actually need it for this problem as we’ll have constexpr). Another alternative is to use Boost.Typeof, in which case we’ll end up with

    #include <boost/typeof/typeof.hpp>  template<typename T> struct ArraySize {     private:    static T x;     public:     enum { size = sizeof(T)/sizeof(*x)}; }; template<typename T> struct ArraySize<T*> {}; 

    and is used by writing

    ArraySize<BOOST_TYPEOF(foo)>::size 

    where foo is the name of an array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 66k
  • Answers 66k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Because the productToEdit is a new product object populated from… May 11, 2026 at 11:27 am
  • added an answer Have a look at JNA. Here is the summary of… May 11, 2026 at 11:27 am
  • added an answer It's something the browsers do, as part of the process… May 11, 2026 at 11:27 am

Related Questions

While refactoring code and ridding myself of all those #defines that we're now taught
While refactoring some old code I have stripped out a number of public methods
Having read Fowler's Refactoring for a while, I still often catch myself thinking I
While the C# spec does include a pre-processor and basic directives (#define, #if, etc),
While going through university and from following the development of SO, I've heard a
While I've seen rare cases where private inheritance was needed, I've never encountered a
While setting up CruiseControl, I added a buildpublisher block to the publisher tasks: <buildpublisher>
While working in a Java app, I recently needed to assemble a comma-delimited list
While in the final throws of upgrading MS-SQL Server 2005 Express Edition to MS-SQL
While Ctrl X works fine in vim under windows, Ctrl A selects all (duh).

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.