包装 C++模板,以便它们可以在 C#、Java 等中使用

发布于 2024-11-24 23:56:28 字数 522 浏览 1 评论 0原文

我有一个 C++ 库 (.lib),它严重依赖于模板。我正在为该库创建一个 C++ 包装器,以在 .dll 中向外部公开其函数。这些公开的函数将依次由 Java 和 C# 库分别通过 JNA/JNI 和 PInvoke 使用。

我从这个SO问题了解到它不是可以导出模板。

有没有人对包装 C++ 模板的最佳方法有很好的指导,以便保留尽可能多的灵活性,但可以导出?

例如,典型的 C++ 函数如下:

template <class A, class B>
inline
A 
do_something(A first, A last, 
              B  result )
{
    /* implementation */
}

I have a C++ library (.lib) which relies heavily on templates. I'm creating a C++ wrapper to this library to expose its functions externally in a .dll. These exposed functions will in turn be used by both Java and C# libraries via JNA/JNI and PInvoke respectively.

I understand from this SO question that it is not possible to export templates.

Does anyone have good guidance on the best way to wrap C++ templates so that as much of their flexibility is retained, but can be exported?

As an example, a typical C++ function would be the following:

template <class A, class B>
inline
A 
do_something(A first, A last, 
              B  result )
{
    /* implementation */
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衾梦 2024-12-01 23:56:28

当人们说“无法导出模板”时,实际上意味着“无法导出模板”。

由于无法导出模板,因此您无法“尽可能保留其灵活性” 您可以导出单个函数,

但不能导出模板。您始终可以从函数模板生成函数(通过实例化它)。templatevoid foo() 是一个模板,但 >无效foo() 是一个函数,尽管它的名称中有一些有趣的符号)

但是,模板的灵活性只是因为它们是模板而存在。也无法导出灵活性,您必须导出要导出的特定函数和类。

When people say "it is not possible to export templates", that does actually mean "it is not possible to export templates.

Since it is not possible to export templates, you have no way to "retain as much as possible of their flexibility".

You can export individual functions, but not templates. You can always generate a function from a function template (by instantiating it. template <typename T> void foo() is a template, but void foo<int>() is a function, albeit one with some funny symbols in its name)

But no, the flexibility of templates is only there because they're templates. When you can't export the template, you can't export the flexibility either. You'll have to export the specific functions and classes that you want exported.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文