WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C++ · Expert · question 62 of 100

Explain the use of SFINAE (Substitution Failure Is Not An Error) in C++ template metaprogramming.?

📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

SFINAE (Substitution Failure Is Not An Error) is a powerful concept in C++ template metaprogramming that allows us to define template functions or classes that are only instantiated if certain conditions are met. The basic idea is that if a substitution fails during template instantiation, it is not treated as a compile-time error but simply results in the template being discarded from consideration.

SFINAE relies on the fact that when the C++ compiler is instantiating a template, it will attempt to substitute the template arguments into the template definition. If the substitution is successful, the template is instantiated and the resulting code is compiled. However, if the substitution fails, the compiler simply discards that instantiation attempt and moves on to the next one.

The SFINAE technique can be used to selectively enable or disable template instantiations based on the types of the arguments passed to the template. For example, consider the following template function that computes the size of an array:

    template <typename T, size_t N>
    size_t array_size(T (&)[N]) {
        return N;
    }

This function uses a reference to an array to determine its size. However, it will only work correctly if the argument passed to it is actually an array. If a pointer or any other type is passed, the template substitution will fail and the function will not be instantiated.

To make the function more robust, we can use SFINAE to selectively enable it only for array types:

    template <typename T>
    struct is_array {
        static const bool value = false;
    };
    
    template <typename T, size_t N>
    struct is_array<T[N]> {
        static const bool value = true;
    };
    
    template <typename T>
    typename std::enable_if<is_array<T>::value, size_t>::type
    array_size(T (&)[N]) {
        return N;
    }

In this version of the function, we define a type trait is_array that is used to determine if a type is an array. We then use the std::enable_if mechanism to selectively enable the template only for array types. If the type is not an array, the substitution will fail and the template will be discarded.

SFINAE is a powerful technique that enables us to write more flexible and robust templates in C++. By selectively enabling or disabling templates based on the types of their arguments, we can ensure that our code is both more efficient and more correct.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C++ interview — then scores it.
📞 Practice C++ — free 15 min
📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

All 100 C++ questions · All topics