引用模板[C++语言]

This topic shows how to use a template that has been previously declared.

template-name < template-arg-list >

备注

The template-arg-list should be a comma-separated list of:

expressiontype-name

All expression arguments must be constant expressions. The compiler creates a new instance (called an instantiation) of the templated class or function if there is no exact match to a previously generated template. For example, to reference the MyStack class defined in 成员函数模板:

MyStack< unsigned long, 5 > stack1;     
// Creates a stack of unsigned longs.
MyStack< DWORD, 5 >stack2;
// Uses code created above.
MyStack< char, 6 > stack3;
// Generates new code.
MyStack< MyClass, 6 > stack4;
// Generates stack of MyClass objects.

Each generated function template creates its own static variables and members.

All template arguments must be accessible at the point where they are used.

The exception to the above syntax rule is in identifying a member template specialization in an expression after the ::, . or -> operators. After these operators, the keyword template can be specified. Visual C++ departs from the standard in that the template keyword is always optional in this context, whereas the standard requires it in some circumstances. The template keyword cannot be used in the specialization unless following these operators.

[ :: | ->| .] template template-name < template-arg-list >

例如,下面的指定调用是类 X 的成员成员函数模板f<T>(int) 的 int 专用化并向其传递参数 10。

X::template f<int>(10);

请参见

其他资源

模板