What does this algorithm do?

Started by
3 comments, last by Hodgman 11 years, 7 months ago
Hey everyone

I was looking at a game engine to see how the different developers structure the code in their projects. I came across this algorithm but cannot figure out what it does exactly.

This algorithm is stored in a header file.

// Static compile-time assertion
[color=#0000ff]namespace StaticAssert
{
[color=#0000ff]template< [color=#0000ff]bool > [color=#0000ff]struct FAILED;
[color=#0000ff]template<> [color=#0000ff]struct FAILED< [color=#0000ff]true > { };
}

[color=#0000ff]#define ASSERT_STATIC( exp ) (StaticAssert::FAILED< (exp) != 0 >())


Can anyone explain what it does? "ASSERT_STATIC" is implemented in a cpp file. Here's the code:

// Check some platform-dependent assumptions
// Note: this function is never called but just wraps the compile time asserts
[color=#0000ff]static void __ValidatePlatform__()
{
ASSERT_STATIC([color=#0000ff] sizeof( int64 ) == 8 );
ASSERT_STATIC( [color=#0000ff]sizeof( [color=#0000ff]int ) == 4 );
ASSERT_STATIC( [color=#0000ff]sizeof( [color=#0000ff]short ) == 2 );
ASSERT_STATIC( [color=#0000ff]sizeof( [color=#0000ff]char ) == 1 );
}


Thank you for reading!
Advertisement
A static assert is the compile-time equivalent to an assert() call in the code; it makes an assumption about the code at that particular point and throws an error if the assumption does not hold. The code you posted uses template code to evaluate expressions at compile time and determine whether some assumptions (in this case, whether an int is 4 bytes for example) are valid, or the compiler will break with an error.
Take the sizeof(int)==4 expression as an example.

  • The macro will expand to a template of the StaticAssert::FAILED template class, passing the expression as a template parameter.
  • The expression will either evaluate to true or false depending on whether an int really is 4 bytes or not. Thus, the macro and template expands to StaticAssert::FAILED<true>() if the expression is true.
  • The extra set of parentheses after the template class name instantiates an instance of the class.
  • The actual template class itself is only forward declared: template<bool> struct FAILED, and is then specialized with a complete definition only when the boolean template parameter is true.

A class can only be instantiated if the full definition is available. Since the template is only declared and not defined if the template parameter is false, the compiler will error out with an error message if the assert expression is false. Thus, the code will not compile if, as in your code, the size of an int64 is not 8 bytes, an int is not 4 bytes and a short is not 2 bytes. The last line, sizeof(char)==1, is redundant since the size of a char is 1 by definition.
Thank you so much. You've explained that perfectly, I had a good Idea about what was going on but you explained the bits I couldn't figure out!
For the record, it's a non-standard implementation of what the [font=courier new,courier,monospace]static_assert[/font] that's a part of the C++ language does, only with emitting the useful diagnostic message.

Stephen M. Webb
Professional Free Software Developer


For the record, it's a non-standard implementation of what the [font=courier new,courier,monospace]static_assert[/font] that's a part of the C++11 language does, only omitting the useful diagnostic message.
FTFY. A lot of game platforms don't support C++11 yet, so it's common to emulate these features via C++03 code.

This topic is closed to new replies.

Advertisement