Call pattern function from pattern class

GCC will not compile the following code snippet (which is actually the correct behavior of GCC, since it conforms to standard C ++, as I already learned. VC ++, however, will compile.)

template<class T> void CUDAMemory1D2DTextureAllocator<T>::allocateMemoryOnDevice() { m_pChannelDesc = cudaCreateChannelDesc<T>(); ... } 

As I already found out in the search, I need to tell the compiler that cudaCreateChannelDesc is a template method. Otherwise, it will try to parse < as smaller than the operator ...

The following snippet shows that in a simple example:

 template< typename G > struct Test { template< typename T > T f() const; }; template< typename G, typename T > void g() { Test< G > t; tf< 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... } 

So far so good. Now my question is: how to do this in the case above, where is the method I call cudaCreateChannelDesc that does not belong to any class or namespace? Any advice or suggestions on how to solve this situation are very welcome.

thanks

+4
source share
3 answers

You can call it directly like this: cudaCreateChannelDesc<T>() if it does not belong to any class or namespace. Doesn't that work?

+2
source

I assume that cudaCreateChannel1Desc not global or a namespace function, because this should work if you haven't forgotten the inclusion permission or namespace. And you say that this is a "method", i.e. Member function.

So, if this is a method of the CUDAMemory1D2DTextureAllocator class, you should use this->template cudaCreateChannel1Desc<T>() to call this method (which I deduced is the CUDAMemory1D2DTextureAllocator template base class CUDAMemory1D2DTextureAllocator . The following is a compact illustration of what works and what doesn't in different situations (at least in gcc):

 template <class G> struct Base { template< class T > T h() const { std::cout << "Hello World!" << std::endl; }; }; template< class G > struct Test : public Base<G> { template< class T > T f() const { std::cout << "Hello World!" << std::endl; }; void callF() const { f<G>(); //works! this->f<G>(); //works! h<G>(); //ERROR! this->h<G>(); //ERROR! this->template h<G>(); //works! }; }; 
+2
source

Is the cudaCreateChannelDesc function declared in any namespace in your scope? It looks like you might have a problem finding two-phase names, which requires that some of the objects (like functions) mentioned in the templates should be visible before the template is instantiated. If you write a non-template function in the same place as the allocateMemoryOnDevice definition and call cudaCreateChannelDesc inside it, does the code compile? If not, you may be missing #include or the namespace qualification on cudaCreateChannelDesc .

+1
source

Source: https://habr.com/ru/post/1336747/


All Articles