template non-type parameter 非类型参数

CUDA v6.5 sample->0_simple->matrixMul 中看到语法:

template  __global__ void
matrixMulCUDA(float *C, float *A, float *B, int wA, int wB)
{
    // function body
}

对于用法:

template 


是很常见的,但对于用法:

template 


却少见。


It's perfectly possible to template a class on an integer rather than a type. We can assign the templated value to a variable, or otherwise manipulate in a way we might with any other integer literal:

unsigned int x = N;


In fact, we can create algorithms which evaluate at compile time (from Wikipedia):

template 
struct Factorial 
{
     enum { value = N * Factorial::value };
};

template <>
struct Factorial<0> 
{
    enum { value = 1 };
};

// Factorial<4>::value == 24
// Factorial<0>::value == 1
void foo()
{
    int x = Factorial<4>::value; // == 24
    int y = Factorial<0>::value; // == 1
}

使用 int 类型模板,可以在编译时确定。例如:

template
struct Vector {
    unsigned char bytes[S];
};

// pass 3 as argument.
Vector<3> test;


更多内容可参考: http://stackoverflow.com/questions/499106/what-does-template-unsigned-int-n-mean

http://stackoverflow.com/questions/24790287/templates-int-t-c




你可能感兴趣的:(CUDA,c++)