Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use template arguments in extern "C"' functions

Tags:

c++

My requirement is to get an item from a collection using extern "c" function. The method will be as below

template<class _Ty,
class _Alloc = allocator<_Ty> >
extern "C" __declspec(dllexport) _Ty* __cdecl GetItem(std::vector<_Alloc>* itr, int index)
{
    if (itr->size() < index)
        return NULL;

    return &itr->at(index);
}

While compiling im getting an error as below

error C2988: unrecognizable template declaration/definition

The use of this extern method is to get object data using pinvoke from c#

like image 712
S.Frank Richarrd Avatar asked Sep 17 '18 12:09

S.Frank Richarrd


1 Answers

No, that's not possible. The standard prohibits templates and their specialisations from having C linkage. Qutoing C++17 (N4659) [temp] 17/4:

... A template, a template explicit specialization (17.7.3), and a class template partial specialization shall not have C linkage. ...

like image 56
Angew is no longer proud of SO Avatar answered Oct 20 '22 00:10

Angew is no longer proud of SO