tc03.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 59 行

C
59
字号
// out of line member functions
template <class T,int size,void (*err)()>
    struct Stack {
	int index;
	static int last;
	T a[size];
	Stack();
	T pop();
	void push( T x );
	void dummy();
    };
template <class T,int size,void (*err)()>
Stack<T,size,err>::Stack() : index(0)
{
};
template <class T,int size,void (*err)()>
int Stack<T,size,err>::last = 1;
template <class T,int __size,void (*err)()>
T Stack<T,__size,err>::pop()
{
    if( index == 0 ) {
	(*err)();
	return -1;
    }
    return a[--index];
}
template <class T,int size,void (*err)()>
void Stack<T,size,err>::push( T x )
{
    if( index == size ) {
	(*err)();
    }
    a[index++] = x;
}
template <class T,int size,void (*err)()>
void Stack<T,size,err>::dummy( void )
{
}

extern "C" int printf( char *, ... );

void ok()
{
    printf( "PASS\n" );
}

Stack<int,10,ok> s;

void main( void )
{
    s.push( 1 );
    s.push( 2 );
    s.pop();
    s.pop();
    if( s.pop() != -1 ) {
	printf( "FAIL\n" );
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?