Using a C++ function template from CLI wrapper











up vote
0
down vote

favorite












I'm building a C++/CLI DLL to be able to communicate a plain C++ DLL with a C# application. The plain C++ DLL is third party provided and I have no way to modify it.



That plain C++ DLL contains some member function templates, in the way:





  1. plain C++ DLL header file:



    class pureCPP
    {
    template<typename T>
    void usefulFunctionA(T &b, T const &a)
    {
    /* Implementation of the function */
    }
    }



So my idea would be to reflect that in the CLI DLL header file:





  1. CLI DLL header file:



    public ref class CLI_DLL
    {
    template<typename T>
    void usefulFunctionB(T &b, T const &a)
    {
    pureCppPtr->usefulFunctionA(b, a);
    }

    PureCPP *pureCppPtr;
    }



And then in the C# executable for example simply call:



CLI_DLL cliDLL = new CLI_DLL;
double a, b;
cliDLL.usefulFunctionB<double>(a, b);


Is this doable? Or am I forced to simply instantiate overloads for every possible type of the function template?



I've read C# has something called generics, could I use that technique to achieve something like this? So far, every article I've found refers either to class templates (not just methods within them) or suggests to do explicit instantiation in the CLI of the desired types, which is what I'd like to avoid if possible (suppose each function can be used by 1000 different types...I guess it wouldn't be so nice to replicate the 1000 different instantiations).



I'm a completely newbie to C#, but AFAIU, passing arguments (in this case, a type) from a generic function to a template wouldn't be doable since the former are resolved at runtime while the latter is done at compile time, in which case I'd be forced to change the previous implementation to something like this:





  • CLI DLL header file:



    public ref class CLI_DLL
    {
    /* Only define the template here */
    template<typename T>
    void usefulFunctionB(T &b, T const &a);

    PureCPP *pureCppPtr;
    }



  • CLI DLL source file:



    /* Explicitly instantiate the template in the source file */
    template<>CLI_DLL::usefulFunctionB<double>(double &A, double const &B)
    {
    pureCppPtr->usefulFunctionA<double>(b, a);
    }



on so on for every type I need. The other way (template to generic) seems to be possible though, right?



(BTW, and as a side question, is it really necessary to explicit the type being called, "double" in this case? I've never really understood why in some cases, Visual Studio is not able to automatically deduct types from templates, as CLang or GCC are).



Which is what I'd really, REALLY liked to avoid, and instead, I'd love to be able to do something like the first code I exposed.



Thanks a lot in advance.










share|improve this question




















  • 1




    There is no way for C# to instantiate C++ template. But generic seems promising. I am afraid that you have to dig deep into C# generic and its cli form.
    – felix
    Nov 21 at 15:55










  • And you might want to keep CLI layer simple. According to my colleagues, debugging in CLI is a nightmare.
    – felix
    Nov 21 at 15:57










  • +1 to @felix 's link. You're running into runtime vs compile time problems here. C# does a lot of its work at runtime (or just-in-time compilation, depending), so working them together isn't easy. While the generic option is there, I'd see what your NEEDS are. The fastest way to "get it done" may be to just write a non-generic wrapper or two to the specific specializations you need. It's not elegant, but it would get the job done. The "generic" option linked is just a bunch of specializations, so not that different really.
    – Kevin Anderson
    Nov 22 at 18:27















up vote
0
down vote

favorite












I'm building a C++/CLI DLL to be able to communicate a plain C++ DLL with a C# application. The plain C++ DLL is third party provided and I have no way to modify it.



That plain C++ DLL contains some member function templates, in the way:





  1. plain C++ DLL header file:



    class pureCPP
    {
    template<typename T>
    void usefulFunctionA(T &b, T const &a)
    {
    /* Implementation of the function */
    }
    }



So my idea would be to reflect that in the CLI DLL header file:





  1. CLI DLL header file:



    public ref class CLI_DLL
    {
    template<typename T>
    void usefulFunctionB(T &b, T const &a)
    {
    pureCppPtr->usefulFunctionA(b, a);
    }

    PureCPP *pureCppPtr;
    }



And then in the C# executable for example simply call:



CLI_DLL cliDLL = new CLI_DLL;
double a, b;
cliDLL.usefulFunctionB<double>(a, b);


Is this doable? Or am I forced to simply instantiate overloads for every possible type of the function template?



I've read C# has something called generics, could I use that technique to achieve something like this? So far, every article I've found refers either to class templates (not just methods within them) or suggests to do explicit instantiation in the CLI of the desired types, which is what I'd like to avoid if possible (suppose each function can be used by 1000 different types...I guess it wouldn't be so nice to replicate the 1000 different instantiations).



I'm a completely newbie to C#, but AFAIU, passing arguments (in this case, a type) from a generic function to a template wouldn't be doable since the former are resolved at runtime while the latter is done at compile time, in which case I'd be forced to change the previous implementation to something like this:





  • CLI DLL header file:



    public ref class CLI_DLL
    {
    /* Only define the template here */
    template<typename T>
    void usefulFunctionB(T &b, T const &a);

    PureCPP *pureCppPtr;
    }



  • CLI DLL source file:



    /* Explicitly instantiate the template in the source file */
    template<>CLI_DLL::usefulFunctionB<double>(double &A, double const &B)
    {
    pureCppPtr->usefulFunctionA<double>(b, a);
    }



on so on for every type I need. The other way (template to generic) seems to be possible though, right?



(BTW, and as a side question, is it really necessary to explicit the type being called, "double" in this case? I've never really understood why in some cases, Visual Studio is not able to automatically deduct types from templates, as CLang or GCC are).



Which is what I'd really, REALLY liked to avoid, and instead, I'd love to be able to do something like the first code I exposed.



Thanks a lot in advance.










share|improve this question




















  • 1




    There is no way for C# to instantiate C++ template. But generic seems promising. I am afraid that you have to dig deep into C# generic and its cli form.
    – felix
    Nov 21 at 15:55










  • And you might want to keep CLI layer simple. According to my colleagues, debugging in CLI is a nightmare.
    – felix
    Nov 21 at 15:57










  • +1 to @felix 's link. You're running into runtime vs compile time problems here. C# does a lot of its work at runtime (or just-in-time compilation, depending), so working them together isn't easy. While the generic option is there, I'd see what your NEEDS are. The fastest way to "get it done" may be to just write a non-generic wrapper or two to the specific specializations you need. It's not elegant, but it would get the job done. The "generic" option linked is just a bunch of specializations, so not that different really.
    – Kevin Anderson
    Nov 22 at 18:27













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm building a C++/CLI DLL to be able to communicate a plain C++ DLL with a C# application. The plain C++ DLL is third party provided and I have no way to modify it.



That plain C++ DLL contains some member function templates, in the way:





  1. plain C++ DLL header file:



    class pureCPP
    {
    template<typename T>
    void usefulFunctionA(T &b, T const &a)
    {
    /* Implementation of the function */
    }
    }



So my idea would be to reflect that in the CLI DLL header file:





  1. CLI DLL header file:



    public ref class CLI_DLL
    {
    template<typename T>
    void usefulFunctionB(T &b, T const &a)
    {
    pureCppPtr->usefulFunctionA(b, a);
    }

    PureCPP *pureCppPtr;
    }



And then in the C# executable for example simply call:



CLI_DLL cliDLL = new CLI_DLL;
double a, b;
cliDLL.usefulFunctionB<double>(a, b);


Is this doable? Or am I forced to simply instantiate overloads for every possible type of the function template?



I've read C# has something called generics, could I use that technique to achieve something like this? So far, every article I've found refers either to class templates (not just methods within them) or suggests to do explicit instantiation in the CLI of the desired types, which is what I'd like to avoid if possible (suppose each function can be used by 1000 different types...I guess it wouldn't be so nice to replicate the 1000 different instantiations).



I'm a completely newbie to C#, but AFAIU, passing arguments (in this case, a type) from a generic function to a template wouldn't be doable since the former are resolved at runtime while the latter is done at compile time, in which case I'd be forced to change the previous implementation to something like this:





  • CLI DLL header file:



    public ref class CLI_DLL
    {
    /* Only define the template here */
    template<typename T>
    void usefulFunctionB(T &b, T const &a);

    PureCPP *pureCppPtr;
    }



  • CLI DLL source file:



    /* Explicitly instantiate the template in the source file */
    template<>CLI_DLL::usefulFunctionB<double>(double &A, double const &B)
    {
    pureCppPtr->usefulFunctionA<double>(b, a);
    }



on so on for every type I need. The other way (template to generic) seems to be possible though, right?



(BTW, and as a side question, is it really necessary to explicit the type being called, "double" in this case? I've never really understood why in some cases, Visual Studio is not able to automatically deduct types from templates, as CLang or GCC are).



Which is what I'd really, REALLY liked to avoid, and instead, I'd love to be able to do something like the first code I exposed.



Thanks a lot in advance.










share|improve this question















I'm building a C++/CLI DLL to be able to communicate a plain C++ DLL with a C# application. The plain C++ DLL is third party provided and I have no way to modify it.



That plain C++ DLL contains some member function templates, in the way:





  1. plain C++ DLL header file:



    class pureCPP
    {
    template<typename T>
    void usefulFunctionA(T &b, T const &a)
    {
    /* Implementation of the function */
    }
    }



So my idea would be to reflect that in the CLI DLL header file:





  1. CLI DLL header file:



    public ref class CLI_DLL
    {
    template<typename T>
    void usefulFunctionB(T &b, T const &a)
    {
    pureCppPtr->usefulFunctionA(b, a);
    }

    PureCPP *pureCppPtr;
    }



And then in the C# executable for example simply call:



CLI_DLL cliDLL = new CLI_DLL;
double a, b;
cliDLL.usefulFunctionB<double>(a, b);


Is this doable? Or am I forced to simply instantiate overloads for every possible type of the function template?



I've read C# has something called generics, could I use that technique to achieve something like this? So far, every article I've found refers either to class templates (not just methods within them) or suggests to do explicit instantiation in the CLI of the desired types, which is what I'd like to avoid if possible (suppose each function can be used by 1000 different types...I guess it wouldn't be so nice to replicate the 1000 different instantiations).



I'm a completely newbie to C#, but AFAIU, passing arguments (in this case, a type) from a generic function to a template wouldn't be doable since the former are resolved at runtime while the latter is done at compile time, in which case I'd be forced to change the previous implementation to something like this:





  • CLI DLL header file:



    public ref class CLI_DLL
    {
    /* Only define the template here */
    template<typename T>
    void usefulFunctionB(T &b, T const &a);

    PureCPP *pureCppPtr;
    }



  • CLI DLL source file:



    /* Explicitly instantiate the template in the source file */
    template<>CLI_DLL::usefulFunctionB<double>(double &A, double const &B)
    {
    pureCppPtr->usefulFunctionA<double>(b, a);
    }



on so on for every type I need. The other way (template to generic) seems to be possible though, right?



(BTW, and as a side question, is it really necessary to explicit the type being called, "double" in this case? I've never really understood why in some cases, Visual Studio is not able to automatically deduct types from templates, as CLang or GCC are).



Which is what I'd really, REALLY liked to avoid, and instead, I'd love to be able to do something like the first code I exposed.



Thanks a lot in advance.







c# c++ templates command-line-interface






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 15:49









Peter Ruderman

10k2252




10k2252










asked Nov 21 at 15:33









A. Palma

627




627








  • 1




    There is no way for C# to instantiate C++ template. But generic seems promising. I am afraid that you have to dig deep into C# generic and its cli form.
    – felix
    Nov 21 at 15:55










  • And you might want to keep CLI layer simple. According to my colleagues, debugging in CLI is a nightmare.
    – felix
    Nov 21 at 15:57










  • +1 to @felix 's link. You're running into runtime vs compile time problems here. C# does a lot of its work at runtime (or just-in-time compilation, depending), so working them together isn't easy. While the generic option is there, I'd see what your NEEDS are. The fastest way to "get it done" may be to just write a non-generic wrapper or two to the specific specializations you need. It's not elegant, but it would get the job done. The "generic" option linked is just a bunch of specializations, so not that different really.
    – Kevin Anderson
    Nov 22 at 18:27














  • 1




    There is no way for C# to instantiate C++ template. But generic seems promising. I am afraid that you have to dig deep into C# generic and its cli form.
    – felix
    Nov 21 at 15:55










  • And you might want to keep CLI layer simple. According to my colleagues, debugging in CLI is a nightmare.
    – felix
    Nov 21 at 15:57










  • +1 to @felix 's link. You're running into runtime vs compile time problems here. C# does a lot of its work at runtime (or just-in-time compilation, depending), so working them together isn't easy. While the generic option is there, I'd see what your NEEDS are. The fastest way to "get it done" may be to just write a non-generic wrapper or two to the specific specializations you need. It's not elegant, but it would get the job done. The "generic" option linked is just a bunch of specializations, so not that different really.
    – Kevin Anderson
    Nov 22 at 18:27








1




1




There is no way for C# to instantiate C++ template. But generic seems promising. I am afraid that you have to dig deep into C# generic and its cli form.
– felix
Nov 21 at 15:55




There is no way for C# to instantiate C++ template. But generic seems promising. I am afraid that you have to dig deep into C# generic and its cli form.
– felix
Nov 21 at 15:55












And you might want to keep CLI layer simple. According to my colleagues, debugging in CLI is a nightmare.
– felix
Nov 21 at 15:57




And you might want to keep CLI layer simple. According to my colleagues, debugging in CLI is a nightmare.
– felix
Nov 21 at 15:57












+1 to @felix 's link. You're running into runtime vs compile time problems here. C# does a lot of its work at runtime (or just-in-time compilation, depending), so working them together isn't easy. While the generic option is there, I'd see what your NEEDS are. The fastest way to "get it done" may be to just write a non-generic wrapper or two to the specific specializations you need. It's not elegant, but it would get the job done. The "generic" option linked is just a bunch of specializations, so not that different really.
– Kevin Anderson
Nov 22 at 18:27




+1 to @felix 's link. You're running into runtime vs compile time problems here. C# does a lot of its work at runtime (or just-in-time compilation, depending), so working them together isn't easy. While the generic option is there, I'd see what your NEEDS are. The fastest way to "get it done" may be to just write a non-generic wrapper or two to the specific specializations you need. It's not elegant, but it would get the job done. The "generic" option linked is just a bunch of specializations, so not that different really.
– Kevin Anderson
Nov 22 at 18:27

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415464%2fusing-a-c-function-template-from-cli-wrapper%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415464%2fusing-a-c-function-template-from-cli-wrapper%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks