DigestCPP

Lets Understand With Example

  • Home
  • Design Principal
  • Design Patterns
  • C++ 11 Features
  • C++11 Multithreading
  • Contact Us

static assert c++11

static assert

It is a new feature in c++11, it does the check of specific condition at compile time. We always prefer compile time error than run time error.

source code with example:

/*
Program: Static Assert 
Author: Alpha Master
Date: 14 March 2021
*/

//Header File
#include<iostream>

const int max_speed = 120;

int main()
{
    std::cout<<"Static Assert"<<std::endl;
    //const int speed = 190;
    //error: static assertion failed: You have crossed the Max speed
    const int speed = 90;
    //static assert takes (!condition, message)
    static_assert(speed<max_speed, "You have crossed the Max speed");
    std::cout<<"Speed:"<<speed<<std::endl;

    return 0;
}

Output:

Static Assert
Speed:90

Primary Sidebar




DigestCPP © 2023. All rights reserved.

    About Privacy Policy Terms and Conditions Contact Us Disclaimer