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.
Table of Contents
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).
1 2 3 |
template< class T > struct is_final |
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.
1 2 3 4 5 6 7 |
class myclass final { }; if( std::is_final<myclass>::value ) std::cout << "myclass is final" << std::endl; |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <iostream> #include <type_traits> class myclass1 { }; class myclass2 final { }; static_assert(std::is_final_v<myclass2> == true); // C++17 int main() { // C++14 if( !std::is_final<myclass1>::value ) std::cout << "myclass1 is NOT final" << std::endl; if( std::is_final<myclass2>::value ) std::cout << "myclass2 is final" << std::endl; // C++17 if( !std::is_final_v<myclass1> ) std::cout << "myclass1 is NOT final" << std::endl; if( std::is_final_v<myclass2> ) std::cout << "myclass2 is final" << std::endl; system("pause"); return 0; } |
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.