Possible Duplicate:
Is it possible to write a C++ template to check for a function's existence?
In languages like JavaScript, you can check for the presence of a property
// javascript
if( object['property'] ) // do something
In C++, I want to condition compilation based on whether the type T has a certain property or not. Is this possible?
template <typename T>
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< vector<int> > intfoo;
  intfoo.add( 500 ) ;
  intfoo.add( 200 ) ;
  intfoo.print() ;
  // expected that the LIST has (200, 500 --end)
  IntFoo< list<int> > listfoo ;
  listfoo.add( 500 ) ;
  listfoo.add( 200 ) ; // it always calls .push_back
  listfoo.print();
}
 
     
     
     
    