Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


What does this algorithm do?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Coro   Members   -  Reputation: 112

Like
1Likes
Like

Posted 29 August 2012 - 05:10 PM

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
namespace StaticAssert
{
template< bool > struct FAILED;
template<> struct FAILED< true > { };
}

#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
static void __ValidatePlatform__()
{
ASSERT_STATIC( sizeof( int64 ) == 8 );
ASSERT_STATIC( sizeof( int ) == 4 );
ASSERT_STATIC( sizeof( short ) == 2 );
ASSERT_STATIC( sizeof( char ) == 1 );
}


Thank you for reading!

Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8626

Like
14Likes
Like

Posted 29 August 2012 - 05:26 PM

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.

#3 Coro   Members   -  Reputation: 112

Like
0Likes
Like

Posted 29 August 2012 - 05:50 PM

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!

#4 Bregma   Crossbones+   -  Reputation: 5482

Like
0Likes
Like

Posted 29 August 2012 - 08:31 PM

For the record, it's a non-standard implementation of what the static_assert that's a part of the C++ language does, only with emitting the useful diagnostic message.
Stephen M. Webb
Professional Free Software Developer

#5 Hodgman   Moderators   -  Reputation: 31938

Like
2Likes
Like

Posted 29 August 2012 - 09:28 PM

For the record, it's a non-standard implementation of what the static_assert 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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS