How should I do this explicit instantiation?

G

Good Guy

I've got following class and template function:

template <size_t num>
class String{
public:
char charArray[num];
};

template <size_t num,typename T>
void getString(String<num> & string,T number){
cout <<string.charArray<<' '<<number<<'\n';
}

then I tried to do an explicit instantiation as following to export
that instantiation to a DLL but found out at last that it didn't get
instantiated at all since I got a linker error of unresolved external
symbol by linker at the place I was about to import and use that
function (exact linker error:"**unresolved external symbol
"__declspec(dllimport)
void __cdecl getString<5>(class String<5> &,unsigned char) (__imp_??
$getString@$04@@YAXAAV?$String@$04@@E@Z)**") because "num" was not
specified at the point I was intending to instantiate; at the first
place I was thinking that maybe because String<num> & string would be
implemented as a pointer the following syntax would've been an
instantiation but seems I was wrong.

template<size_t num>
__declspec(dllexport) void getString(String<num> & string,unsigned
char number);

Now how do you suggest I should do the instantiation because I'm not
certainly going to do it for every single integer number found on
earth!!!.
 
V

Victor Bazarov

I've got following class and template function:

template<size_t num>
class String{
public:
char charArray[num];
};

template<size_t num,typename T>
void getString(String<num> & string,T number){
cout<<string.charArray<<''<<number<<'\n';
}

then I tried to do an explicit instantiation as following to export
that instantiation to a DLL but found out at last that it didn't get
instantiated at all since I got a linker error of unresolved external
symbol by linker at the place I was about to import and use that
function (exact linker error:"**unresolved external symbol
"__declspec(dllimport)
void __cdecl getString<5>(class String<5> &,unsigned char) (__imp_??
$getString@$04@@YAXAAV?$String@$04@@E@Z)**") because "num" was not
specified at the point I was intending to instantiate; at the first
place I was thinking that maybe because String<num> & string would be
implemented as a pointer the following syntax would've been an
instantiation but seems I was wrong.

template<size_t num>
__declspec(dllexport) void getString(String<num> & string,unsigned
char number);

Now how do you suggest I should do the instantiation because I'm not
certainly going to do it for every single integer number found on
earth!!!.

An explicit instantiation starts with the keyword 'template' that has
*no template arguments* following it. Not even an angle bracket should
follow. So, your instantiation (modulo MS-specific stuff) should read

template void getString(String<5> &, unsigned char);

, I think. I've not tried it, though.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top