GCC 不会编译以下代码片段(这实际上是 GCC 的正确行为,因为它符合我已经了解到的标准 C++。但是 VC++ 会编译。)
template<class T>
void CUDAMemory1D2DTextureAllocator<T>::allocateMemoryOnDevice()
{
m_pChannelDesc = cudaCreateChannelDesc<T>();
...
}
正如我已经通过搜索发现的那样,需要告诉编译器 cudaCreateChannelDesc
是一个模板方法。否则它会尝试
解析 <
作为小于运算符...
下面的代码片段在一个简单的例子中展示了这一点:
template< typename G >
struct Test
{
template< typename T > T f() const;
};
template< typename G, typename T >
void g()
{
Test< G > t;
t.f< T >(); // ERROR: gcc won't compile that
t.template f< T >(); // OK: now gcc knows that f is a template method an treads the following angle brackets not as operators but as template brackets...
}
到目前为止一切顺利。现在我的问题是,在上述情况下如何做到这一点,我调用的方法是 cudaCreateChannelDesc
哪个不属于任何类或命名空间?
非常欢迎任何关于如何解决这种情况的意见或建议。
谢谢
最佳答案
您可以像这样直接调用它:cudaCreateChannelDesc<T>()
如果它不属于任何类或 namespace 。不是这样吗?
关于c++ - 从模板类中调用模板函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4779401/