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 49887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:30:26+00:00 2026-05-10T16:30:26+00:00

I’m tidying up some older code that uses ‘magic numbers’ all over the place

  • 0

I’m tidying up some older code that uses ‘magic numbers’ all over the place to set hardware registers, and I would like to use constants instead of these numbers to make the code somewhat more expressive (in fact they will map to the names/values used to document the registers).

However, I’m concerned that with the volume of changes I might break the magic numbers. Here is a simplified example (the register set is more complex):

const short mode0 = 0; const short mode1 = 1; const short mode2 = 2;  const short state0 = 0; const short state1 = 4; const short state2 = 8; 

so instead of :

set_register(5); 

we have:

set_register(state1|mode1); 

What I’m looking for is a build time version of:

ASSERT(5==(state1|mode1)); 

Update

@Christian, thanks for the quick response, I’m interested on a C / non-boost environment answer too because this is driver/kernel code.

  • 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-10T16:30:26+00:00Added an answer on May 10, 2026 at 4:30 pm

    NEW ANSWER :

    In my original answer (below), I had to have two different macros to support assertions in a function scope and at the global scope. I wondered if it was possible to come up with a single solution that would work in both scopes.

    I was able to find a solution that worked for Visual Studio and Comeau compilers using extern character arrays. But I was able to find a more complex solution that works for GCC. But GCC’s solution doesn’t work for Visual Studio. 🙁 But adding a ‘#ifdef __ GNUC __’, it’s easy to choose the right set of macros for a given compiler.

    Solution:

    #ifdef __GNUC__ #define STATIC_ASSERT_HELPER(expr, msg) \     (!!sizeof \ (struct { unsigned int STATIC_ASSERTION__##msg: (expr) ? 1 : -1; })) #define STATIC_ASSERT(expr, msg) \     extern int (*assert_function__(void)) [STATIC_ASSERT_HELPER(expr, msg)] #else     #define STATIC_ASSERT(expr, msg)   \     extern char STATIC_ASSERTION__##msg[1]; \     extern char STATIC_ASSERTION__##msg[(expr)?1:2] #endif /* #ifdef __GNUC__ */ 

    Here are the error messages reported for STATIC_ASSERT(1==1, test_message); at line 22 of test.c:

    GCC:

    line 22: error: negative width in bit-field `STATIC_ASSERTION__test_message' 

    Visual Studio:

    test.c(22) : error C2369: 'STATIC_ASSERTION__test_message' : redefinition; different subscripts     test.c(22) : see declaration of 'STATIC_ASSERTION__test_message' 

    Comeau:

    line 22: error: declaration is incompatible with         'char STATIC_ASSERTION__test_message[1]' (declared at line 22) 

     
     

    ORIGINAL ANSWER :

    I do something very similar to what Checkers does. But I include a message that’ll show up in many compilers:

    #define STATIC_ASSERT(expr, msg)               \ {                                              \     char STATIC_ASSERTION__##msg[(expr)?1:-1]; \     (void)STATIC_ASSERTION__##msg[0];          \ } 

    And for doing something at the global scope (outside a function) use this:

    #define GLOBAL_STATIC_ASSERT(expr, msg)   \   extern char STATIC_ASSERTION__##msg[1]; \   extern char STATIC_ASSERTION__##msg[(expr)?1:2] 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 109k
  • Answers 109k
  • 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
  • Editorial Team
    Editorial Team added an answer Google Checkout and PayPal both differ from "traditional" payment processing… May 11, 2026 at 9:21 pm
  • Editorial Team
    Editorial Team added an answer And a Perl one-liner you get! perl -MList::Util -e 'print… May 11, 2026 at 9:21 pm
  • Editorial Team
    Editorial Team added an answer It sounds like your startup projects are all set to… May 11, 2026 at 9:21 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

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.