Is C++ dynamic or static?

Table of Contents

It is a kind of language whose structure can be changed at runtime: for example, new functions, objects, and even codes can be introduced, existing functions can be deleted, or other structural changes. In layman’s terms, the code can change its structure according to certain conditions at runtimeStatic language.
Corresponding to dynamic languages, languages ​​with immutable runtime structures are static languages, such as Java, C, C++, C#.
PS: C# is not a dynamic language, but MS tends to support dynamic languages ​​. NET. 3.0 has absorbed certain dynamic features, such as anonymous functions, temporary types, temporary variables, etc.
Dynamically typed language
Dynamically typed languages and dynamic languages are two completely different concepts. Dynamically typed language: refers to a language that does data type checking only during runtime, and it refers to data types. The data type of a dynamically typed language is not determined at the compilation stage, but the type binding is delayed to the runtime stage.
Main languages: Python, Ruby, Erlang, JavaScript, Swift, PHP, Perl.

2. Statically typed languages
The data type of a static language is determined during compilation (or before running), and the data type of the variable must be determined when writing the code.
Main languages: C, C++, C#, Java, Object-C.

Click this affiliate link to learn more about C++

Is C++ dynamic or static
code-944499_1280 (1)

Difference between static and dynamic type checking

Programming languages following the type of examination can be divided into two categories: static type (Static Typing) and dynamic type (Dynamic Typing). Among the more popular languages, static types include Java, C/C++, Golang, etc., and dynamic types include Python, Ruby, etc.

What is the difference between static type and dynamic type? Why should we consider static or dynamic in programming language design? When writing code, Python is simple and efficient to write. It may be possible to complete a 100-line Java program in 10 lines of Python, so I used to think that a statically typed language was a bit too rigid, not as good as a dynamically typed one. But after learning programming languages ​​during this period, one cannot say that one is better than the other; each has its own merits. This article compares static and dynamic types from 3 angles and 6 aspects.

Why is there a concept of static typing/dynamic typing?

All programs need to be checked for errors. For example, 3 / 0, this program will have errors; when should we check it?

When writing a program, as long as the division by 0 appears in the editor, an error will be reported.

When compiling, if the division by 0 is detected, an error will be reported.

When the program is running, it runs to divide by 9, and an error is reported.

No error is reported, and infinity (+inf.0) is returned.

Different language designs will choose to report errors at different times during these four processes. The difference between static type and dynamic type is when the type error is reported? For example, 3 / “a” static types are mostly at compile-time, and dynamic types are mostly at program runtime. How to report a type of error? There will be a type checking mechanism in the language. The purpose of type checking is to avoid something happening in the program.

A programming language designed to consider to check what type of program? , How to perform type checking? Static typing and dynamic typing are the product of different answers to these two questions.

Comparison from the perspective of writing code


Static typing is better: Static typing is more convenient because there is no need to check whether x is a number;* can only be a number by default.

Dynamic typing is better: Dynamic typing is more convenient because it can return different types as needed. Static typing needs to construct a new data type to achieve.

What are Static typing vs. dynamic typing pros and cons

Writing code is the most creative and satisfying part of creating software from scratch. However, to make sure the code works, you need to analyze it. For this, we have the Dynamic Code Analysis and its static analysis. Which of the two types of analysis is more convenient?

What is dynamic code analysis?

Before beginning to differentiate between the two types of analysis, it is convenient to clarify that code analysis is a code review process through which it is intended to evaluate said code. This evaluation involves the search for operating problems of the same code and aims to improve its operation.

This code analysis can be performed in two ways: dynamic code analysis or static code analysis can be carried out.

Static analysis has the advantage over dynamic analysis in that it is done without executing the code. As it does not require such execution, static analysis allows errors to be detected very early in writing. This saves a lot of time in later stages of development. The most serious problem that it offers instead is that it can throw positives that are not and whose falsehood will only be seen during the execution of the code.

Dynamic code analysis is performed while the code is running. It is slower and requires a complete testing process. However, it allows you to see many errors that are hidden in a static analysis.

The testing process for dynamic code analysis

Both types of code analysis mentioned support full testing processes. There are different types of them specially designed for dynamic code analysis and static analysis. In the latter case, a good process of reviewing the documentation and maintaining it is recommended. Above all, for the benefit of developers working on large projects. In this type of work, chances are the order of the day and, without adequate documentation, it is impossible to maintain an overview of the project.

Types of tests for dynamic code analysis

Black box: The objective of these tests is to verify that the outputs are correct. No attention is paid to the way in which these outputs are made. Modular independence is taken into account for easier implementation of each module. This makes it easier to address the failure.

White box: focus on procedural failures related to inputs. The method usually consists of making all the possible inputs to obtain a specific output. This type of testing should be modified every time the implementation changes in the project.

Is C++ statically typed?

We hope that C++ should be statically type-safe, which can reduce runtime errors and improve the code’s robustness. However, due to the existence of the following features of C++, C++ static type safety will be destroyed. We must deal with these features carefully.


Type conversion cast

Narrowing conversions

Type decay

Range errors

void*Type pointers can solve these problems by restricting the use of these features or using new features of C++, such as variant (C++17), GSL span, narrow_cast, etc., and improve the robustness of C++ code.

Follow the C++ISO standard

I hope to write C++ code by using the features of the ISO C++ standard. The features that are not defined in the ISO standard or implemented by the compiler should be used with caution, and the extended features provided by compilers such as GCC should also be used with caution. These features will lead to the code. The portability is relatively low.

Note: If you need to use related extended features in the module, you can encapsulate these features into independent interfaces as much as possible, and you can turn off or compile these features through the compiler option. For the use of these extended features, the module should develop a feature programming guide to guide the use of these features.

Check for errors during compilation first.

The compiler gives priority to ensuring the robustness of the code, rather than writing error handling code to handle exceptions that can be found by compilation, such as:

Use const to ensure the invariance of data and prevent data from being unintentionally modified.

Use gsl::span to ensure that the char array does not cross the boundary instead of passing the length check at runtime.

Use static_assert to check at compile time.

C++ other features

Constant values ​​are easier to understand, track, and analyze, so you should use constants instead of variables as much as possible. When defining values, you should use const as the default option.

Recommendation 9.1.1 Do not allow the use of macros to represent constants.

Note: Macro is a simple text replacement, which is completed in the preprocessing stage, and the corresponding value is reported directly when running an error; the value is also displayed during tracking and debugging, instead of the macro name; the macro has no type check and is unsafe; the macro has no scope.

Modern C++ features

With the release of the C++11 language standard by ISO in 2011 and the release of C++17 in March 2017, modern C++ (C++11/14/17, etc.) has added many new languages ​​that improve programming efficiency and code quality Features and standard library. This chapter describes some guidelines that can help teams use modern C++ more efficiently and avoid language traps.

Is Python dynamically typed?

First of all, we must be clear about the standard definition of the distinction between statically typed languages ​​and dynamically typed languages. If type checking occurs at compile-time, then it is in statically typed languages. Conversely, if type checking occurs at runtime (run time), then dynamically typed languages.

What is type checking?

Type checking is to check the type of the variable and then judge that the expression is reasonable. It can be understood as follows: The compiler usually does type checking (Java) during the compilation stage, and the interpreter usually does type checking (Python)

Static language: All variable types must be explicitly declared because this information is needed at the compilation stage, for example, in Java.

Dynamic (Dynamic) language: Display declaration is not required because type assignment occurs at runtime, for example, in Python.

From the above analysis, we know that Python is a dynamic language (type checking occurs at the runtime, and there is no need to display the declared type), so what is the criterion for distinguishing strong and weak types?

Let’s start with the concept: strongly typed languages ​​have a stronger type checking mechanism, and strict type checking will be done in expression calculation, while weakly typed languages ​​allow some operations between various variable types.

Let’s look at the example again: First, let’s look at a substantial type. In a strong type, whether at compile time or run time, once a type is assigned to a variable, it will hold this type and cannot calculate a certain type with other types—mixed calculations when using multiple expressions.

Python has for some time established itself as one of the leaders in the market for programming languages ​​for Data Science: easy to learn for people with a past as a statistician, well supplied with libraries, capable of everything, it is a faithful companion of the Data Scientist.

However, we can do Data Science at a fairly high level without seeing everything of its particularities: how many Data Scientists use decorators? Is Python multi-threaded or multi-core?

Now the other day, trying to answer the question of why the following code shows two different results,


Type checking ensures that the variable type in an expression is legal. In statically typed languages, type checking occurs in the compilation phase; in dynamically typed languages, type checking occurs in the runtime phase.

Difference between static and dynamic typing in Python

Programming languages ​​are divided into static types and dynamic types and strong types, and weak types. They are two sets of vocabulary convenient for describing language characteristics, but they are confused in use. This article will give you a clear and concise introduction to popular science.

1. Static Type VS Dynamic Type

To understand the difference between static and dynamic, we have to start with variable assignment operations. In a statically typed language, the type of a variable must be declared first; that is, the variable is determined at the moment of creation. In use, you can only assign data of this specified type to the variable. If you forcibly assign other irrelevant types of data to it, an error will occur.

Once a variable is declared to be of type int, you can only assign data of type int to it in a static language. Otherwise, it will cause an error, while dynamic type has no such restriction, what type of data you assign to the variable, What type is this variable.

The following languages ​​are all dynamic types:

  • PHP
  • Ruby
  • Python
  • Common statically typed languages ​​are:
  • C
  • C++
  • JAVA
  • C#
  • 2. Strong Type VS Weak Type

The difference between the strong and the weak is reflected in the strictness of the type checking. The weakly typed language checks the variable type relatively loosely and tolerates the occurrence of implicit type conversion. What is implicit type conversion, generally has two forms:

  • Implicit conversion between related types
  • Implicit conversion between unrelated types

For example, if an int type data is added to a float type data, the final result is float type data. This process undergoes implicit type conversion. The int type data is first converted to float type and then Operates with another float; this is an implicit conversion between related types.

An int type data is added to a string type data, and no error occurs. The result is a string. The int type data is implicitly converted to a string, but they were initially two unrelated data types. It is the second implicit conversion.

A variable can be implicitly cast to an unrelated type in a weakly typed language, but in a strongly typed language, it cannot. According to the definition of this concept, PHP and Perl are weakly typed languages, and other programming languages, such as Java, C, C++, and Python, are strongly typed languages.

Click this affiliate link to learn more about Python.

Is Javascript dynamically typed?

The variable type is known at compile time as a static type; the variable type known at runtime is called a dynamic type. Such as:

The compiler int age = 18; will compile the age determined by the type of code when, in other words, you can not divide by zero operation him and so on, because the type itself defines the operational collection; but like C ++ in common auto ite = vec. Iterator (); This is also a static type. This type is called type inference. The type of the unknown variable is derived at compile time from the known type. A syntax error will be reported in a statically typed language for a variable not allowed by the variable type.

But as var name = student.getName(); this line of JavaScript code is dynamically typed because this line of code is executed only when the name is to know the type of string or even null undefined type. You can’t make type inference because of the student.getName function signature does not contain the return value type information. Later, we will introduce some other means to add types to function signatures. Performing operations on a variable not allowed by the variable type in the dynamic type will report a runtime error.

The strong type does not allow implicit conversion, and the weak type allows implicit conversion. Such as:

In Python, ‘666’ / 2you’ll get a type error because a strongly typed language is not allowed implicit conversion, carried out in JavaScript, ‘666’ / 2you’ll get integer 333, this is because when the operation is performed in a string of ‘666’ It is first converted into an integer 666, and then the division operation is performed.

Program Errors

Trapped errors: causes the program to terminate execution (program awareness to the error, use error handling mechanism corresponding), such as division by zero, an array of cross-border access in Java.

Click this affiliate link to learn more about Java.

Static vs. dynamic type Java

Static type checking: the method of verifying type safety supported the ASCII text file of the program;

dynamic type checking: the method of verifying type safety during program operation;

Java uses static type checking to research the program during compilation to ensure that there are no type errors. The essential idea isn’t to let type errors occur during runtime.

First, what is going to be returned by calling new B().me()? Object A or B?

Me () method is asserted to return an A object, so the compiler only knows that it returns an A object during compilation. However, it returns the B object during operation because B inherits the tactic of A and returns itself.

The following line of code is unlawful, whether or not the B object’s strategy doB() is termed. The matter is that its reference type may be a. When compiling the device, the compiler doesn’t know its real type, so it’s thought to be a kind.

Association of the method call to the tactic body is thought as binding. There are two varieties of binding: Static Binding that happens at compile-time, and Dynamic Binding happens at runtime. Before I explain static and dynamic binding in Java, let’s see few terms that may better understand this idea.

Static and Dynamic Binding in Java

As mentioned above, an association of method definition to the strategy call is understood as binding. There are two kinds of binding: Static binding and dynamic binding. Let’s discuss them.

Static Binding or Early Binding

The compiler’s binding, which might be resolved at compile-time, is thought of as static or early binding. The binding of static, private, and final methods is compile-time. Why? The rationale is that these methods can’t be overridden, and therefore the kind of category is decided by the compile time.


After these comparisons, we can see that static types and dynamic types have their strengths, and one cannot merely and roughly say that one is better than the other. I think that dynamic types are more suitable for smaller programs, such as Python and Ruby, as scripting languages, which can quickly and easily write file processing. Dynamically typed Java and C++ can support large software engineering projects.

Of course, the specific choice of static type or dynamic type depends on when you want to do type checking? What kind of language features do you want, and you should know what the selected trade-off is.

Static typing has many advantages, and it is also positive for dynamically typed languages. Modern statically typed languages ​​are continually improving their simplicity and convenience, and the gap with dynamically typed languages ​​is narrowing, so they are more close to the people. In recent years, many modern statically typed languages ​​have emerged and become famous. Besides, due to the advantages of static typing, many dynamically typed languages ​​are also introducing static typing support. It can be seen that static typing is a trend in the development of modern languages.

Luis Gillman
Luis Gillman

Hi, I Am Luis Gillman CA (SA), ACMA
I am a Chartered Accountant (SA) and CIMA (SA) and author of Due Diligence: A strategic and Financial Approach.

The book was published by Lexis Nexis on 2001. In 2010, I wrote the second edition. Much of this website is derived from these two books.

In addition I have published an article entitled the Link Between Due Diligence and Valautions.

Disclaimer: Whilst every effort has been made to ensure that the information published on this website is accurate, the author and owners of this website take no responsibility  for any loss or damage suffered as a result of relience upon the information contained therein.  Furthermore the bulk of the information is derived from information in 2018 and use therefore is at your on risk. In addition you should consult professional advice if required.