title: c++ conditional compilation based on member presence
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-template-to-check-for-a-functions-existence">Is it possible to write a C++ template to check for a function&#39;s existence?</a>  </p>
  </blockquote>
  
  
  
  <p>In languages like JavaScript, you can check for the presence of a property</p>
  
  <pre><code>// javascript
  if( object['property'] ) // do something
  </code></pre>
  
  <p>In C++, I want to condition compilation based on whether the type <code>T</code> has a certain property or not. Is this possible?</p>
  
  <pre><code>template &lt;typename T&gt;
  class IntFoo
  {
    T container ;
  public:
    void add( int val )
    {
      // This doesn't work, but it shows what I'm trying to do.
      // if the container has a .push_front method/member, use it,
      // otherwise, use a .push_back method.
      #ifdef container.push_front
      container.push_front( val ) ;
      #else
      container.push_back( val ) ;
      #endif
    }
  
    void print()
    {
      for( typename T::iterator iter = container.begin() ; iter != container.end() ; ++iter )
        printf( "%d ", *iter ) ;
  
      puts( "\n--end" ) ;
    }
  } ;
  
  int main()
  {
    // what ends up happening is 
    // these 2 have the same result (500, 200 --end).
    IntFoo&lt; vector&lt;int&gt; &gt; intfoo;
    intfoo.add( 500 ) ;
    intfoo.add( 200 ) ;
    intfoo.print() ;
  
    // expected that the LIST has (200, 500 --end)
    IntFoo&lt; list&lt;int&gt; &gt; listfoo ;
    listfoo.add( 500 ) ;
    listfoo.add( 200 ) ; // it always calls .push_back
  
    listfoo.print();
  }
  </code></pre>