Compilers are tools that are used to convert the high-level codes we write into machine codes so that computers or microprocessors can run them comfortably, and during this conversion process, they are tools that aim to obtain maximum efficiency from the system thanks to various optimization processes. Since the systems running in the background are complex, it is often overlooked, but there are some optimization processes that we need to do as well as the optimization levels that compilers provide us. In this series of articles, I will be talking about the optimizations made automatically by the compiler and the optimizations that the developer can make.

From a developer’s point of view, compilers seem to do all the work automatically. For example; The GCC compiler has optimization flags that you can use during the compilation process. Thanks to them, you can set the level at which you want to optimize your code. Using the maximum optimization flag may not always work for you, you can use the compiler’s documentation page for detailed information on this subject. Although the compilers perform some optimization operations on their own, as the size and complexity of the code increases as the project grows, at some points we may need to make some additions and deletions in the code in order to prevent the compiler from being unstable and to save compilation time.

The subject of this article will be the noexcept statement that comes with the concept of Modern C++.

noexcept

The noexcept statement is an expression introduced with C++11 and performs a compile-time check that returns true if it is declared that the function in which it is used will not throw any exceptions.

Well what does it mean? Software design is based on a method called “exception safety guarantee”;

1) Basic Guarantee
2) Strong Guarantee
3) Nothrow Guarentee

Whatever the code we will write, “Basic Guarantee” must be provided as a minimum. However, in cases where a “Nothrow Guarantee” is provided, it is useful to point this out to the compiler. If noexcept is not used while writing the code, the compiler spends time and resources to predict the expections that may occur in the code. When it is used, we inform the compiler that “Nothrow” is automatically provided. This is a serious waste of time in large projects. In the project I am currently in, we can see the benefit of this in compilation time. If you are sure that no exception will be thrown in the functions you create, if you add the noexcept statement to the function, the compiler will pass this check, thus saving time and resources. However, it is necessary to be careful when using it, because it will cause run-time problems if it is done without making sure that the function will not throw an exception.

Example usage:

void no_throw() noexcept;

For more information: C++ Reference | Noexcept

 

Author

Genel dünya problemleri ile çok ilgili olmasa da teknolojik gelişmeleri yakından takip eden, sistemleri geliştirmek için çalışmalar yapan, bolca kod yazmaya çalışan ve öğrendiklerini paylaşmaya çalışan birisi.

Write A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.