Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
Understanding static_assert in C++ 11
Next article icon

Understanding static_assert in C++ 11

Last Updated : 08 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

What is static assertion?

Static assertions are a way to check if a condition is true when the code is compiled. If it isn't, the compiler is required to issue an error message and stop the compiling process. The condition that needs to be checked is a constant expression.
  • Performs compile-time assertion checking
  • Syntax:
    static_assert( constant_expression, string_literal ); 

How has static assertion been done prior to C++ 11 standard?

Prior to the C++ 11 standard the primary mean of producing a compile-time error message was via the #error directive, which causes the implementation to produce a diagnostic message that includes the message that followed it. For example: CPP
// Static assertion using #error directive #include <iostream> using namespace std; #if !defined(__geeksforgeeks) #error "Geeksforgeeks hasn't been defined yet". #endif int main() {     return 0; } 
Error: Geeksforgeeks hasn't been defined yet.

What is the problem with #error directive?

The #error directive works well with simple tasks. However, it fails when compile time assertion needs to be done for complex tasks such as checking the size of a data type using the sizeof operator. It is because the tokens such as 'sizeof' are not converted to source tokens until after the preprocessing translation stage at which point you can no longer use preprocessing directives.

How to do static assertion since C++ 11 standard?

The C++ 11 standard introduced a feature named static_assert() which can be used to test a software assertion at the compile time. Syntax:
static_assert( constant_expression, string_literal );    Parameters:  constant_expression: An integral constant expression   that can be converted to a Boolean.  string_literal: The error message that is displayed   when the constant_expression parameter is false. 
The 'constant_expression' parameter represents a software assertion (a condition that you expect to be true at a particular point in your program) that needs to be checked during the compile time. If the condition is true, the static_assert declaration has no effect. If the condition is false, the assertion fails, the compiler displays the message in string_literal parameter and the compilation fails with an error. It is, however, important to note that the string_literal parameter is optional. Example: CPP
// CPP code to demonstrate // static assertion using static_assert #include <iostream> using namespace std;  template <class T, int Size> class Vector {     // Compile time assertion to check if     // the size of the vector is greater than     // 3 or not. If any vector is declared whose     // size is less than 4, the assertion will fail     static_assert(Size > 3, "Vector size is too small!");      T m_values[Size]; };  int main() {     Vector<int, 4> four; // This will work     Vector<short, 2> two; // This will fail      return 0; } 
Output:
error: static assertion failed: Vector size is too small!
In the above code, we've created a template class named vector where we don't wanna allow a vector to be created whose size is less than 4. Therefore, inside the template body, we put a static_assert statement to check if the vector being made is of size>3. In case of failure, the assertion will fail with the error message: " Vector size is too small". This is exactly what happens in the declaration of the vector class object 'two'. The size passed to it is '2', which fails the condition to be checked, therefore producing the compile time error, and thus halting the compilation process.

What are the advantages of static_assert over #error?

  1. Unlike #error, assertion using static_assert takes place after the preprocessing translation stage. Therefore, it is possible to check for the size of a datatype with sizeof using static_assert. CPP
    // Datatype #include <iostream>  using namespace std; // No error produced. // The program compiles well because // the size of long datatype is 8 bytes static_assert(sizeof(long) == 8,  ode relies on 'long' being exactly 8 bytes"); int main() {     return 0; } 
  2. Libraries can detect common usage errors at compile time.
  3. Implementations of the C++ Standard Library can detect and diagnose common usage errors, improving usability.

Declaration Scopes

static_assert can be used in namespace scope, class scope, as well as block scope. The examples of each of the aforementioned scopes are as follows:
  1. Namespace scope: CPP
    // CPP program to illustrate // declaring static_assert in namespace scope #include <iostream> static_assert(sizeof(void*) == 8,  "DTAMDL(*LLP64) is not allowed for this module."); int main() {     cout << "Assertion passed.      The program didn't produce an error";     return 0; } 
    Output:
    assertion passed. The program didn't produce an error
  2. Class scope: CPP
    // CPP program to illustrate class scope in // static assertion using static_assert #include <iostream> using namespace std;  template <class T, int Size> class Vector {     // Compile time assertion to check if     // the size of the vector is greater than     // 3 or not. If any vector is declared whose     // size is less than 4, the assertion will fail     static_assert(Size > 3, "Vector size is too small!");      T m_values[Size]; };  int main() {     Vector<int, 4> four; // This will work     Vector<short, 2> two; // This will fail      return 0; } 
    Output:
    error: static assertion failed: Vector size is too small!
  3. Block scope: CPP
    // CPP program to illustrate // declaring static_assert in block scope template <typename T, int N> void f() {     static_assert(N >= 0, "length of array a is negative.");     T a[N]; } int main() {     // assertion fails here     // because the length of the array passed     // is below 0     f<int, -1>();     return 0; } 
    Output:
    error: static assertion failed: length of array a is negative.

Erroneous static_assert

The constant_expression passed in static_assertion needs to be a valid expression. For example, consider the following code: CPP
// CPP program to illustrate // demonstrating an erroneous static_assert declaration int main() {     static_assert(1 / 0, "never shows up!");     return 0; } 
Output:
prog.cpp:5:2: error: non-constant condition for static assertion    static_assert(1 / 0, "never shows up!");    ^
In the above piece of code, the expression '1/0' is not a valid constant expression. Therefore, When this is compiled, instead of showing the string literal in the static_assert declaration, the compiler issues an error message indicating that the divisor must not be zero.

Next Article
Understanding static_assert in C++ 11

P

ParthDutt
Improve
Article Tags :
  • C++
Practice Tags :
  • CPP

Similar Reads

    Assertions in C/C++
    Assertions are statements used to test assumptions made by programmers, such as validating logic or invariants in the code. For example, we may use an assertion to check if the index of an array is within valid bounds during development. Following is the syntax for assertion.void assert( int express
    2 min read
    std is_union() template in C++
    The std::is_union template of C++ STL is used to check whether the given type is union or not. It returns a boolean value showing the same. Syntax: template <class T> struct is_union; Parameter: This template accepts single parameter T (Trait class) to check whether T is a union or not. Return
    2 min read
    ios operator() function in C++11 with Examples
    The operator() method of ios class in C++11 is used to any error flag of this stream is set. This includes the failbit or the badbit. Syntax: explicit operator bool() const; Parameters: This method does not accept any parameter. Return Value: This method returns false if any error bit is set of this
    1 min read
    Strict Type Checking in C++
    Strict type checking means the function prototype(function signature) must be known for each function that is called and the called function must match the function prototype. It is done at compile time. The "strictly typed language" refers to a very strongly typed language in which there are more s
    4 min read
    noexcept Operator in C++ 11
    Exception handling is an important concept in C++. Keywords like 'try', 'catch', and 'throw' are often associated with exception handling. Although these keywords are very powerful on their own we need another more efficient method to operate on them. Hence, noexcept operator was introduced in C++ 1
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences