JavaScriptのような言語では、プロパティの存在を確認できます
// javascript
if( object['property'] ) // do something
T
C ++では、型に特定のプロパティがあるかどうかに基づいてコンパイルを条件付けたいと思います。これは可能ですか?
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();
}