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

  • SEARCH
  • Home
  • 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 1070335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:32:45+00:00 2026-05-16T20:32:45+00:00

I am using an aggregate initializer to set up a block of static data

  • 0

I am using an aggregate initializer to set up a block of static data for a unit test.

I would like to use the array size as the expected number of elements, but this can fail if too few initializers are provided:

my_struct_type expected[14] =
{
    { 1.234, 0, 'c' },
    { 3.141, 1, 'z' },
    { 2.718, 0, 'a' }
};

This gives no compiler error in Visual Studio 2008.

I would like to be able to use it as such:

const unsigned expected_size = sizeof(expected) / sizeof(my_struct_type);

BOOST_CHECK_EQUAL(points.size(), expected_size);

for( int i = 0; i < expected_size; i++ )
{
    BOOST_CHECK_EQUAL(points[i].value, expected[i].value);
    BOOST_CHECK_EQUAL(points[i].count, expected[i].count);
    BOOST_CHECK_EQUAL(points[i].sym,   expected[i].sym);
}

but because I don’t have a compile-time guarantee of 14 points, this runs off the end of the array end of the provided values and into the default-initialized values.

Can I somehow enforce the number of aggregate array initializers at compile-time?

  • 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. Editorial Team
    Editorial Team
    2026-05-16T20:32:45+00:00Added an answer on May 16, 2026 at 8:32 pm

    First: There might be a warning for this. Have you tried compiling at the highest warning level?

    Then: If you swap which value is calculated and which is literal, you could raise a compile-time error:

    my_struct_type my_array[] = // <== note the empty []
    {
        { 1.234, 0, 'c' },
        { 3.141, 1, 'z' },
        { 2.718, 0, 'a' }
    };
    
    BOOST_STATIC_ASSERT( sizeof(my_array)/sizeof(my_array[0]) == 14 );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using an aggregate control in some reports you would prefer to see a
I'm using reshape in R to compute aggregate statistics over columns of a data.frame.
I am using GROUP BY to aggregate over an item per month. I would
In R I frequently aggregate daily data (in a zoo ) by month, using
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
Using JDeveloper , I started developing a set of web pages for a project
Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
How do i aggregate(using linq) the value of one column of a Dataset where
I have to query for total amount of a column using an aggregate function.

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.