Template definition cannot find my function

I am trying to pass some code written for MSVC to compile on Clang. However, there seems to be a problem with some template features. Here is the part of defining the structure that gives me problems:

template< typename T > struct FixedVector { T * ptr; size_t count; typedef T value_type; FixedVector():ptr(0),count(0) {} ~FixedVector() { DeleteArr(ptr); // Error message appears here count=0; } // ... } 

The DeleteArr(ptr) function refers to a function defined later, for example:

 template< typename T > inline void DeleteArr( T *& ptr ) { delete[] ptr; ptr = NULL; } 

This is the error message I get on the specified line:

 error: call to function 'DeleteArr' that is neither visible in the template definition nor found by argument-dependent lookup 

Looking at the full dropdown list of errors (in Xcode), at the bottom of the list is the following message:

 'DeleteArr' should be declared prior to the call site or in an associated namespace of one of its arguments. 

Clicking on this post leads me to define the DeleteArr () function, as shown above.

This seems to compile fine in MSVC, and looking back at the differences between Clang and MSVC, this is due to the quirk in MSVC, which does not require such functions to be defined before they are used, as long as there is a definition somewhere. So I looked at this error message in the Clang documentation (the corresponding part is under the heading “Unqualified search in templates”), and she suggested adding ahead before defining a template. So I added this above definition for FixedVector :

 template< typename T > inline void DeleteArr( T *& ptr ); 

However, this error message still appears, and the last bit of the error message ("must be declared before the call node battle") indicates the actual definition of the function. Does anyone know what could be the problem? I do not know how this works on MSVC. Also, since an error message may find a function definition, why does it say that it cannot be found?

UPDATE: as recommended by the comments, I added the DeleteArr () implementation to where I declared it above the template. This seems to lead to the same error! Now I'm really at a standstill.

+4
source share
2 answers

The DeleteArr must be accessible by the FixedVector , i.e. the definition of the former must precede its use by the latter. This is likely due to the fact that MSVC does not perform a correct two-phase search search .

+5
source

first define DeleteArr

 template< typename T > inline void DeleteArr( T *& ptr ) { delete[] ptr; ptr = NULL; } 

Then we define a fixed vector

 template< typename T > struct FixedVector { T * ptr; size_t count; typedef T value_type; FixedVector():ptr(0),count(0) {} ~FixedVector() { DeleteArr(ptr); // Error message appears here count=0; } // ... } 
+1
source

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


All Articles