template non-type parameter非タイプパラメータ

1473 ワード

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