Open In App
Related Articles

Strict Type Checking in C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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 strict restrictions as to how operations can be performed on variables of different types. 

Basic Terms:

  • for each operation: The number, order, and data types of its arguments.
  • for each variable: Name and datatype of each object.
  • for each constant: Name, datatype, and value.

Generally, a strongly typed language has stricter typing rules at compile-time, which implies that errors and exceptions are more likely to happen during compilation, which increases the reliability of the delivered program, “the compiler generates errors if types don’t match up”.

The function prototype specifies the function name and number of arguments passed and the return type ( if any) of the function.

For Example:

void max ( int , int );
int max ( int , int );
void max();

1. Compiler warns the users if a function is called with improper data types. It helps the user to identify errors in a function call and increases the reliability of a program.

2. Checked Execution Path: As in strict type checking all possible execution paths are checked and further testing of type errors is not needed. So type tags on data objects at run-time are not needed, and no dynamic checking is required. 

C++

#include <iostream>
using namespace std;
void add(int a, double b)
{
    // printing a and b
    cout << "sum = " << (a + b);
}
void add(double a, int b)
{
    // printing a and b
    cout << endl << "sum = " << (a + b);
}
int main()
{
    // calling function on line 3
    add(10, 2.5);
 
    // calling function on line 7
    add(5.5, 6);
    return 0;
}

                    

Output:

sum = 12.5
sum = 11.5

Time complexity: O(1)
Auxiliary Space: O(1)

Based on this, C++ is a strongly typed language and it uses strict type checking. There is no confusion regarding the compiler, which function to call because it is matching the types of arguments at compile time.

In C++, function prototyping is compulsory. If the function declaration is not placed before the function call it will raise an error whereas in C programming language it is optional. C++ checks all the parameters passed to a function against its prototype declaration during compilation. It produces errors if there is a mismatch in its argument type and this can be overcome by placing the prototype of the function.

C++

#include <iostream>
using namespace std;
 
void add(int a, double b)
{
    // printing a and b.
    cout << "sum=" << (a + b);
}
 
void add(double a, int b)
{
    // printing a and b.
    cout << endl << "sum=" << (a + b);
}
 
int main()
{
    // calling add function on line 4.
    add(10, 2.5);
 
    // calling add function with 3
    // parameters that will give
    // error.
    add(5.5, 6, 7);
    return 0;
}

                    


Error:

prog.cpp: In function ‘int main()’:
prog.cpp:24:18: error: no matching function for call to ‘add(double, int, int)’
     add(5.5, 6, 7);
                  ^
prog.cpp:4:6: note: candidate: void add(int, double)
 void add(int a, double b)
      ^
prog.cpp:4:6: note:   candidate expects 2 arguments, 3 provided
prog.cpp:10:6: note: candidate: void add(double, int)
 void add(double a, int b)
      ^
prog.cpp:10:6: note:   candidate expects 2 arguments, 3 provided

Time complexity: O(1)
Auxiliary Space: O(1)

This makes an error, as the function parameters do not match with the number of arguments in the function add. The benefit of strict type checking is that it allows many type errors to be caught early in the development cycle. Strict typing usually results in compiled code that executes more quickly because when the compiler knows the exact data types that are in use, it can produce optimized machine code.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!


Last Updated : 23 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads