tc02.c

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

C
45
字号
// inline functions inside
template <class T,int size,void (*err)()>
    struct Stack {
	int index;
	T a[size];
	Stack() : index(0)
	{
	}
	T pop()
	{
	    if( index == 0 ) {
		(*err)();
		return -1;
	    }
	    return a[--index];
	}
	void push( T x )
	{
	    if( index == size ) {
		(*err)();
	    }
	    a[index++] = x;
	}
    };

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 + -
显示快捷键?