I want to declare a "safe" push()
function for use with auto_ptr
like this:
template<class StackType,typename T>
inline void push( StackType &s, auto_ptr<T> p ) {
s.push( p.get() );
p.release();
}
I also want it to work for null pointers, e.g.:
push( my_stack, 0 ); // push a null pointer
Hence, a specialization:
template<class StackType>
inline void push( StackType &s, int p ) {
s.push( reinterpret_cast<typename StackType::value_type>( p ) );
}
While it works, it's both ugly and allows erroneous code like:
push( my_stack, 1 ); // ???
to compile.
How can I write a specialization of push()
such that it accepts only 0
as a valid int
value (for the null pointer)?
Requirements
StackType
is some stack-like container class that I must use and whose source code I can not change (just likestd::stack
). I can assume it has apush()
member function.I can not use
nullptr
since I can not require a C++0x compiler.