push()
次のように使用する「安全な」関数を宣言したいauto_ptr
:
template<class StackType,typename T>
inline void push( StackType &s, auto_ptr<T> p ) {
s.push( p.get() );
p.release();
}
また、null ポインターに対しても機能するようにします。たとえば、次のようになります。
push( my_stack, 0 ); // push a null pointer
したがって、専門化:
template<class StackType>
inline void push( StackType &s, int p ) {
s.push( reinterpret_cast<typename StackType::value_type>( p ) );
}
動作しますが、醜く、次のような誤ったコードを許可します。
push( my_stack, 1 ); // ???
コンパイルする。
有効な値 (null ポインターの場合)としてpush()
のみ受け入れるような特殊化を作成するにはどうすればよいですか?0
int
要件
StackType
は、使用する必要があり、ソース コードを変更できないスタックのようなコンテナー クラスです(のようにstd::stack
)。push()
メンバー関数があると仮定できます。nullptr
C++0x コンパイラを要求できないため、使用できません。