Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double-templated function instantiation fails

The following code:

template<typename T, MyEnum K> __global__ void myKernel(const T a[]);
template<typename T> __global__ void myKernel<T,SomeValueOfMyEnum>(const T a[]) {
    // implementation
}

Triggers the following error message:

error: an explicit template argument list is not allowed on this declaration

Why?

Notes:

  • I'm pretty sure this isn't CUDA-related, just a C++ issue.
  • There are a bunch of questions on partial specialization, but I can't figure out if mine is a dupe of any of them.
like image 773
einpoklum Avatar asked Nov 13 '13 16:11

einpoklum


Video Answer


2 Answers

You can't do a partial specialization for a template function, because C++ doesn't define such a thing. You just can do a class template partial specialization [§14.5.5 / temp.class.spec]

Class partial specialization -- A little ugly but maybe it helps you.

enum MyEnum
{
    E1, E2
};

template<typename T, MyEnum K>
struct MyKernel
{
    void operator()(const T a[])
    {
        // ...
    }
};

template<typename T>
struct MyKernel<T, E1>
{
    void operator()(const T a[])
    {
        // ...
    }
};

int main()
{
    MyKernel<int, E1>()( ... ); // <--- To call
} 
like image 95
masoud Avatar answered Sep 22 '22 10:09

masoud


You could use enable_if to achieve the goal.

//template<typename T, MyEnum K> __global__ void myKernel(const T a[]);

template<typename T, MyEnum K>
typename std::enable_if<std::is_same<K, SomeValueOfMyEnum>::value, __global__ void>::type
myKernel<T,SomeValueOfMyEnum>(const T a[]) 
{
     // implementation
}

template<typename T, MyEnum K>
typename std::enable_if<!std::is_same<K, SomeValueOfMyEnum>::value, __global__ void>::type
myKernel<T,SomeValueOfMyEnum>(const T a[]) 
{
     // implementation
}
like image 29
procr Avatar answered Sep 19 '22 10:09

procr