__if_exists Statement


The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at __if_exists Statement.

The __if_exists statement tests whether the specified identifier exists. If the identifier exists, the specified statement block is executed.

__if_exists ( identifier ) {   


identifierThe identifier whose existence you want to test.
statementsOne or more statements to execute if identifier exists.
System_CAPS_ICON_caution.jpg Caution

To achieve the most reliable results, use the __if_exists statement under the following constraints.

  • Apply the __if_exists statement to only simple types, not templates.

  • Apply the __if_exists statement to identifiers both inside or outside a class. Do not apply the __if_exists statement to local variables.

  • Use the __if_exists statement only in the body of a function. Outside of the body of a function, the __if_exists statement can test only fully defined types.

  • When you test for overloaded functions, you cannot test for a specific form of the overload.

The complement to the __if_exists statement is the __if_not_exists statement.

Notice that this example uses templates, which is not advised.

// the__if_exists_statement.cpp  
// compile with: /EHsc  
#include <iostream>  
template<typename T>  
class X : public T {  
   void Dump() {  
      std::cout << "In X<T>::Dump()" << std::endl;  
      __if_exists(T::Dump) {  
      __if_not_exists(T::Dump) {  
         std::cout << "T::Dump does not exist" << std::endl;  
class A {  
   void Dump() {  
      std::cout << "In A::Dump()" << std::endl;  
class B {};  
bool g_bFlag = true;  
class C {  
   void f(int);  
   void f(double);  
int main() {   
   X<A> x1;  
   X<B> x2;  
   __if_exists(::g_bFlag) {  
      std::cout << "g_bFlag = " << g_bFlag << std::endl;  
   __if_exists(C::f) {  
      std::cout << "C::f exists" << std::endl;  
   return 0;  

In X<T>::Dump()  
In A::Dump()  
In X<T>::Dump()  
T::Dump does not exist  
g_bFlag = 1  
C::f exists  

Selection Statements
__if_not_exists Statement