6

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.
einpoklum
  • 118,144
  • 57
  • 340
  • 684

2 Answers2

7

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
} 
masoud
  • 55,379
  • 16
  • 141
  • 208
  • 2
    But, but... (Eyes tearing up) [why?!](http://www.freespirit.com/files/IMAGE/COVER/LARGE/ButWhyCantI.jpg) – einpoklum Nov 13 '13 at 17:02
  • I don't know maybe at the future they define it, for now we haven't it. Maybe it makes some inconsistency in the standard! Who knows!?. – masoud Nov 13 '13 at 17:05
  • I just read [this](http://stackoverflow.com/a/8061811/1593077), where it says what I'm trying to do doesn't really count as partial specialization. Or did I misread? – einpoklum Nov 13 '13 at 17:07
  • No, in that code OP was trying to overload functions but you're trying to do a partial specialization – masoud Nov 13 '13 at 17:10
2

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
}
procr
  • 583
  • 1
  • 5
  • 14