How to Check if a Template Class has the Given Member Function in C++? Last Updated : 31 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++ programming, we often need to check if a template class contains a specific member function. In this article, we will discuss how to check if a Template Class has the given member function or not. Check for the Member Function in a Template ClassWe can check whether a member function exists in the template class or not by using SFINAE along with the type traits feature of C++. SFINAE is a technique used in C++ template programming that allows a function template specialization to be ignored if its template argument fails to substitute correctly. This technique is mainly used for creating type traits or to perform compile-time introspection. C++ Program to Check for a Member Function in a Template ClassThe following example demonstrates how we can check whether a template class has a member function or not. C++ // C++ program to check whether a templated class has a // member function or not. #include <iostream> using namespace std; // Define a template class 'temp' template <typename T> class temp { public: // Member function 'func' that returns a bool bool func(const T& element) { return true; } }; // Define a template class 'check' to test for the existence // of 'func' template <typename T> class check { // Define two types of character arrays, 'yes' and 'no', // for SFINAE test typedef char yes[1]; typedef char no[2]; // Test if class T has a member function named 'func' // If T has 'func', this version of test() is chosen template <typename C> static yes& test(decltype(&C::func)); // Fallback test() function used if T does not have // 'func' template <typename> static no& test(...); public: // Static constant 'value' becomes true if T has 'func', // false otherwise The comparison is based on the size // of the return type from test() static const bool value = sizeof(test<T>(0)) == sizeof(yes); }; int main() { // Test if 'temp<int>' has a member function 'func' cout << boolalpha << check<temp<int> >::value << endl; // Outputs true // Test if 'temp<double>' has a member function 'func' cout << boolalpha << check<temp<double> >::value << endl; // Outputs true return 0; } Outputtrue true Explanation: In the above example, we use a technique called SFINAE (Substitution Failure Is Not An Error) to check if a templated class temp contains a member function func. This is achieved using a helper class check. check differentiates between types with and without func using two test functions and the sizeof operator. This method determines if func exists in temp for different types like int and double, as demonstrated in the main function. Comment More infoAdvertise with us Next Article How to Create a Template Class in C++? H harishcarpenter Follow Improve Article Tags : C++ Programs C++ cpp-template C++-Templates CPP Examples +1 More Practice Tags : CPP Similar Reads How to Create a Function Template in C++? In C++, templates enable us to write generic programs that handle any data type. We can create a template class, function, and variable. A template function is a function that can work with any data type. In this article, we will learn how to create a function template in C++. Create a Function Temp 2 min read How to Create a Template Class in C++? In C++, template classes are used to create generic classes that can work for different data types. In this article, we will learn how to create a template class in C++. Create a Template Class in C++ To create a template class in C++, we can follow the below syntax: Syntax of Template Classtemplate 2 min read How to Check if a Vector Contains a Given Element in C++? In C++, a vector is a dynamic array that provides a resizable and efficient way to store elements. Vectors store their elements in contiguous memory locations, similar to arrays, which allows for efficient access and iteration.In this article, we will learn the different methods to check whether the 3 min read How to Check If a Set Contains an Element in C++? In C++, the std::set container stores the unique elements in the sorted order. In this article, we will learn how to check if the set contains a given element or not.ExamplesInput: s = {1, 3, 5, 7, 9}, x = 5Output: Element foundExplanation: The element 5 is present in the set.Input: s = {10, 20, 30, 4 min read Calling a non-member function inside a class in C++ Member Function: It is a function that can be declared as members of a class. It is usually declared inside the class definition and works on data members of the same class. It can have access to private, public, and protected data members of the same class. This function is declared as shown below: 3 min read How to Overload the Function Call Operator () in C++? In C++, operator overloading allows the user to redefine the behavior of an operator for a class. Overloading the function call operator () allows you to treat objects like functions enabling them to be called as if they were functions. Such classes are called functors in C++. In this article, we wi 2 min read Like