CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2023
    Posts
    10

    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.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,867

    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)

  3. #3
    Join Date
    Feb 2024
    Posts
    9

    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
    }

  4. #4
    Join Date
    Aug 2023
    Posts
    10

    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
  •  





Click Here to Expand Forum to Full Width

Featured