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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:46:27+00:00 2026-05-11T17:46:27+00:00

I’d like to implement an assert that prevents compilation, rather than failing at runtime,

  • 0

I’d like to implement an “assert” that prevents compilation, rather than failing at runtime, in the error case.

I currently have one defined like this, which works great, but which increases the size of the binaries.

#define MY_COMPILER_ASSERT(EXPRESSION) switch (0) {case 0: case (EXPRESSION):;}

Sample code (which fails to compile).

#define DEFINE_A 1
#define DEFINE_B 1
MY_COMPILER_ASSERT(DEFINE_A == DEFINE_B);

How can I implement this so that it does not generate any code (in order to minimize the size of the binaries generated)?

  • 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-11T17:46:27+00:00Added an answer on May 11, 2026 at 5:46 pm

    A compile-time assert in pure standard C is possible, and a little bit of preprocessor trickery makes its usage look just as clean as the runtime usage of assert().

    The key trick is to find a construct that can be evaluated at compile time and can cause an error for some values. One answer is the declaration of an array cannot have a negative size. Using a typedef prevents the allocation of space on success, and preserves the error on failure.

    The error message itself will cryptically refer to declaration of a negative size (GCC says “size of array foo is negative”), so you should pick a name for the array type that hints that this error really is an assertion check.

    A further issue to handle is that it is only possible to typedef a particular type name once in any compilation unit. So, the macro has to arrange for each usage to get a unique type name to declare.

    My usual solution has been to require that the macro have two parameters. The first is the condition to assert is true, and the second is part of the type name declared behind the scenes. The answer by plinth hints at using token pasting and the __LINE__ predefined macro to form a unique name possibly without needing an extra argument.

    Unfortunately, if the assertion check is in an included file, it can still collide with a check at the same line number in a second included file, or at that line number in the main source file. We could paper over that by using the macro __FILE__, but it is defined to be a string constant and there is no preprocessor trick that can turn a string constant back into part of an identifier name; not to mention that legal file names can contain characters that are not legal parts of an identifier.

    So, I would propose the following code fragment:

    /** A compile time assertion check.
     *
     *  Validate at compile time that the predicate is true without
     *  generating code. This can be used at any point in a source file
     *  where typedef is legal.
     *
     *  On success, compilation proceeds normally.
     *
     *  On failure, attempts to typedef an array type of negative size. The
     *  offending line will look like
     *      typedef assertion_failed_file_h_42[-1]
     *  where file is the content of the second parameter which should
     *  typically be related in some obvious way to the containing file
     *  name, 42 is the line number in the file on which the assertion
     *  appears, and -1 is the result of a calculation based on the
     *  predicate failing.
     *
     *  \param predicate The predicate to test. It must evaluate to
     *  something that can be coerced to a normal C boolean.
     *
     *  \param file A sequence of legal identifier characters that should
     *  uniquely identify the source file in which this condition appears.
     */
    #define CASSERT(predicate, file) _impl_CASSERT_LINE(predicate,__LINE__,file)
    
    #define _impl_PASTE(a,b) a##b
    #define _impl_CASSERT_LINE(predicate, line, file) \
        typedef char _impl_PASTE(assertion_failed_##file##_,line)[2*!!(predicate)-1];
    

    A typical usage might be something like:

    #include "CAssert.h"
    ...
    struct foo { 
        ...  /* 76 bytes of members */
    };
    CASSERT(sizeof(struct foo) == 76, demo_c);
    

    In GCC, an assertion failure would look like:

    $ gcc -c demo.c
    demo.c:32: error: size of array `assertion_failed_demo_c_32' is negative
    $
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 128k
  • Answers 128k
  • 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 Take a look at this particular screencast. It's a very… May 12, 2026 at 5:46 am
  • Editorial Team
    Editorial Team added an answer PHP/Curl will handle the https request just fine. What you… May 12, 2026 at 5:46 am
  • Editorial Team
    Editorial Team added an answer Changing "\w" to "." should do it: Regex.IsMatch(Password, "^(?=.*[0-9])(?=.*[a-zA-Z]).{8,}$") \w… May 12, 2026 at 5:46 am

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
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

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.