Site icon Learn C++

How Can We Use The is_final Type Trait In C++ 14?

How Can We Use The is final Type Trait In C++ 14

In C++11, the final specifier is used for a function or for a class that cannot be overridden by derived classes, and there was no way to check if that class or method is the final. In C++14, there is a std::is_final type trait that can be used to detect if a class or a method is marked as a final or not. In this post, we explain how we can use the std::is_final type trait in C++14 and C++17.

What is the final specifier in modern C++?

The final specifier (keyword) is used for a function or for a class that cannot be overridden by derived classes. Regarding virtual overrides, C++11 tends to tighten the rules, to detect some problems that often arise. To achieve this goal C++11 introduced a new contextual keyword, the final specifier. The final keyword specifies that a method cannot be overridden, or a class cannot be derived. If you want to learn more about it, here it is,

What is the std::is_final type trait in C++ 14?

The std::is_final type trait (UnaryTypeTrait) defined in <type_traits> detects if a class is marked final and returns true or false boolean. If a class or method is final, it returns the member constant value equal to true, if not returns the value is false.

Here is the syntax (since C++14).

[crayon-664c8b425eb19737627328/]

How can we use the std::is_final type trait in C++ 14?

We can use the std::is_final type trait to check classes if it is marked as a final or not. Here is a simple example.

[crayon-664c8b425eb20635273306/]

Is there a full example about the std::is_final type trait ?

Here is a full example about to use the std::is_final type trait in C++14 and C++17.

[crayon-664c8b425eb23998563840/]

For more information, please see https://cplusplus.github.io/LWG/issue2112

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Exit mobile version