AlwaysLearning
AlwaysLearning

Reputation: 8011

Where is static_assert implemented?

The way static_assert was implemented in the Loki library (where it was actually a pre-processor macro called STATIC_CHECK), it could be used as follows:

STATIC_CHECK(myCondition, My_Error_Message); 

Note that My_Error_Message must be a valid identifier.

I am wondering how static_assert is implemented for the C++11 standard to take a string literal argument for the error message.

I decided to look into the type_traits include file, which is located in /usr/include/c++/4.8 on my Ubuntu 14.04 with g++ 4.8.2. To my surprise, I found only usages of static_assert there, but not the definition (nor pre-processor macro). I did not find it in the files included from type_traits either.

So, where do I look for the implementation of static_assert?

Upvotes: 4

Views: 2968

Answers (2)

edmz
edmz

Reputation: 8494

static_assert has to inherently be built into the compiler because the condition your checking must be checked at compile-time. If it were to be checked with some library code, this would rather be done at runtime.

On the flip-side, assert is a macro, which already says it's implemented in library code, and its check is done at runtime.

Upvotes: 0

templatetypedef
templatetypedef

Reputation: 372814

static_assert is a new language-level feature in C++11, rather than a library included in a header file. A compliant C++ implementation is free to implement static_assert however it likes. It could be built into the compiler (I suspect most compilers do this), or it could be a part of a library (though this would be challenging, since static_assert doesn't require a header file). I think the best way to find out which it is for your particular compiler would be to check the documentation and, if necessary, to look over the source.

Upvotes: 5

Related Questions