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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:52:39+00:00 2026-05-19T15:52:39+00:00

This is a part of a static assert trick. I can’t understand how the

  • 0

This is a part of a static assert trick. I can’t understand how the unspecialized class works. Can someone explain it to me?

EDIT: Full code with macro: (taken from http://www.skynet.ie/~caolan/Fragments/C++StaticAssert.html)

#ifndef STATICASSERT_HXX
#define STATICASSERT_HXX
/*
 Lifted direct from:
 Modern C++ Design: Generic Programming and Design Patterns Applied
 Section 2.1
 by Andrei Alexandrescu
*/
namespace ww
{
    template<bool> class compile_time_check
    {
    public:
        compile_time_check(...) {}
    };

    template<> class compile_time_check<false>
    {
    };
}

    /*
    Similiar to assert, StaticAssert is only in operation when NDEBUG is not
    defined. It will test its first argument at compile time and on failure
    report the error message of the second argument, which must be a valid c++
    classname. i.e. no spaces, punctuation or reserved keywords.
    */
#ifndef NDEBUG
#   define StaticAssert(test, errormsg)                         \
    do {                                                        \
        struct ERROR_##errormsg {};                             \
        typedef ww::compile_time_check< (test) != 0 > tmplimpl; \
        tmplimpl aTemp = tmplimpl(ERROR_##errormsg());          \
        sizeof(aTemp);                                          \
    } while (0)
#else
#   define StaticAssert(test, errormsg)                         \
    do {} while (0)
#endif

#endif
  • 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-19T15:52:39+00:00Added an answer on May 19, 2026 at 3:52 pm

    The macro calls this code in a way similar to this:

    compile_time_check<static expression> temp(Error_Some_Struct_here);
    

    So, for example, you could do this:

    compile_time_check<sizeof(Foo) < sizeof(Bar)> temp(Error_Foo_must_be_smaller_than_Bar);
    

    When sizeof(Foo) is less than sizeof(Bar), the template would instanciate the unspecialized version:

    template<bool> class compile_time_check
    {
    public:
        compile_time_check(...) {}  //What is this?
    };
    

    and the code basically "compiles to" an instanciation of this class:

    compile_time_check temp(Error_Foo_must_be_smaller_than_Bar);
    

    which, being empty and doing nothing, the compiler can remove as dead code. Bam, no runtime overhead, done.

    If, on the other hand, sizeof(Foo) is greater than or equal to sizeof(Bar), it would instead instanciate the specialized version:

    template<> class compile_time_check<false>
    {
    };
    

    and it would attempt to call the constructor compile_time_check::compile_time_check(struct), but since it doesn’t exist, its a compile error. Which is what you want, because static assert should only compile if the assert is true.

    The reason for the constructor to take a variadic parameter list is, I believe, two-fold:

    1. To make sure that it does not call the default constructor, which the specialized version would have. Its variadic so that you can pass in any struct as an error "string". Alternatively, this could have been templated and the constructor could have taken a template object as an argument instead.
    2. So that the error message can be passed in. When the assert is true, this is ignored and nothing happens and the code is removed by the compilers optimizer. If the assert is false, however, the error string should show up in the error message. Something like constructor not found for compile_time_check::compile_time_check(ERROR_Assertion_error_blah()) perhaps.

    An alternative, template-free (I believe its often used in C), static assert I’ve seen somewhere before is this:

    #define compile_time_assert(pred) switch(0){case: 0: case pred:;}
    

    This works because if pred is false, the code would end up as switch(0){case: 0: case 0:;} and two case labels with the same constant is an error. In depth explanation here.

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

Sidebar

Related Questions

How can I extract the part of this stream (the one named BLABLABLA) from
Before anyone suggests scrapping the table tags altogether, I'm just modifying this part of
I'm trying to reteach myself some long forgotten math skills. This is part of
This is a part algorithm-logic question (how to do it), part implementation question (how
Note : The code in this question is part of deSleeper if you want
I'm working on a document application and part of this application I've to add
I'm writing an app to help facilitate some research, and part of this involves
I'm working on a internal web based tool for my company. Part of this
This is actually a two part question. First,does the HttpContext.Current correspond to the current
This is a two part question. A dumb technical query and a broader query

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.