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:
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:
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
add a comment |
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:
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:
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
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
add a comment |
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:
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:
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
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:
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:
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
c# c++ templates command-line-interface
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
add a comment |
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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