Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) static_assert

 

An static_assert is a keyword that can be used, depending on the standard used:

 

 

 

 

 

C++98 static_assert in the C++98 standard

 

static_assert is not supported in C++98:

 

int main()
{
  static_assert(1==2,"One is not equal to two"); //Fails in C++98
}

 

Compiler output:

 

/MyFolder/main.cpp:3: warning: identifier 'static_assert' will become a keyword in C++0x
/MyFolder/main.cpp:: In function 'int main()':
/MyFolder/main.cpp:3: error: 'static_assert' was not declared in this scope

 

BOOST_STATIC_ASSERT does already allow for compile-time assertions in C++98.

 

 

 

 

 

C++0x static_assert in the C++0x standard

 

 

static_assert is a keyword to perform assertions during compile-time.

 

int main()
{
  static_assert(1==2,"One is not equal to two");
}

 

Compiler output:

 

/MyFolder/main.cpp:: In function 'int main()':
/MyFolder/main.cpp:3: error: static assertion failed: "One is not equal to two"

 

Technical note: the code shown is compiled successfully using the G++ 4.4.5 compiler, which is supplied with the Qt Creator 2.0.0 IDE.

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict