-
November 21st, 2024, 12:58 AM
#1
export function using template
Hi,
i like to make the pBuf array type generic like double or int and so. I tried it but was getting various errors. like Fnc not defined in .def.
In header I got:
__declspec(dllexport) void __stdcal My Fnc(int *pBuf);
.cpp:
__declspec(dllexport) void __stdcal My Fnc(int *pBuf)
{
//do stuff with the array
retutn 0;
}
also got .def.
Last edited by J2Int; November 21st, 2024 at 01:00 AM.
-
November 21st, 2024, 04:50 AM
#2
Re: export function using template
With templates, you cannot split function definition and function body across different compile units. They need to be in the same file.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 22nd, 2024, 03:44 PM
#3
Re: export function using template
It looks like you're trying to make the array type generic but running into some issues with the function declaration. If you want to use a generic type like int or double for pBuf, you can template the function.
template <typename T>
__declspec(dllexport) void __stdcall MyFnc(T* pBuf)
{
// Do stuff with the array
}
-
November 25th, 2024, 01:50 AM
#4
Re: export function using template
Due to the tool that uses the API I have to include the MyFunc in the def file. When doing so, using the above approach compiler complains about including the undefined name in def file.
So, i did this in header:
Code:
template <typename T>
void MyFnc(T* pBuf)
{
// Do stuff with the array
}
__declspec(dllexport) void __stdcall MyFncDbl(double* pBuf)
{
return MyFnc(pBuf);
}
etc.
I included MyFncDbl in the def file.
Is this acceptable?.
Last edited by J2Int; November 25th, 2024 at 01:54 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|