Search code examples
c++c++11finaltype-traits

How to detect if a class is final in C++11?


Code first.

#include <iostream>

using namespace std;

struct A final {};
struct B {};

int main()
{ 
    cout << is_final<A>::value << endl; // Output true
    cout << is_final<B>::value << endl; // Output false

    return 0; 
}

How to implement the class is_final?


Solution

  • Type traits are usually implemented using the SFINAE idiom, which places a potentially ill-formed expression inside a function template declaration. Substituting the typename in question into the declaration results in an error, but the error is suppressed in that context, so the declaration is either used or not. But a fallback overload backs up the potentially missing declaration. Another bit of code accesses the function to detect whether the sensitive overload or only the backup was instantiated.

    This won't work for final because it can only cause failure during template instantiation of a class. There's no way to overload classes, and no way to tentatively define a class that will fail but not halt compilation in case it's derived from final.

    Standard quote, C++11 §14.8.2/8:

    Only invalid types and expressions in the immediate context of the function type and its template parameter types can result in a deduction failure. [ Note: The evaluation of the substituted types and expressions can result in side effects such as the instantiation of class template specializations and/or function template specializations, the generation of implicitly-defined functions, etc. Such side effects are not in the “immediate context” and can result in the program being ill-formed. — end note ]