C++符号修改和导出 =>允许代码重复吗?
在我们的项目中,我们有这样的东西:
struct PointI
{
// methods for getting, setting and calculating some point stuff
private:
int x;
int y;
};
struct PointD
{
// methods for getting, setting and calculating some point stuff
private:
double x;
double y;
};
我建议将其更改为类似的东西:
template<typename T>
struct Point
{
// methods for gettig, setting and calculating some point stuff
private:
T x;
T y;
};
typedef Point<int> PointI;
typedef Point<double> PointD;
typedef Point<float> PointF;
但是被拒绝了,我被告知:“这种方法有一个问题 - C++ 符号修改和导出。模板在使用时太长了导出的符号(使用它们的 API)并且无法导出模板。”
这个论点是否如此强大以至于允许大量代码重复?
In our project we have something like this:
struct PointI
{
// methods for getting, setting and calculating some point stuff
private:
int x;
int y;
};
struct PointD
{
// methods for getting, setting and calculating some point stuff
private:
double x;
double y;
};
I proposed to change that into something like that:
template<typename T>
struct Point
{
// methods for gettig, setting and calculating some point stuff
private:
T x;
T y;
};
typedef Point<int> PointI;
typedef Point<double> PointD;
typedef Point<float> PointF;
But that was refused and I was told: "There is one problem with this approach - C++ symbol mangling and exporting. Templates are so long when used in exported symbols (API that uses them) and there is no way how to export templates."
Is that argument so strong to allow lot of code duplication?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的老板(或其他什么人)可能是对的。如果您编写的库应该可以在 C++ 以外的其他语言中使用,那么通常最好只用 C 编写接口。
当然,您仍然可以在内部使用模板,只是不要公开它们。
Your boss (or whatever) is probably right. If you write a library that should be usable from other languages than C++, it is generally a good idea to write the interface in C only.
Of course, you can still internally use templates, just don't expose them.
这仅适用于 C 链接(即,如果导出时使用 extern“C”)。从共享库导出模板类没有技术问题——只需考虑从 C++ 运行时库导出的 STL 类即可。
确实,由于 CPP 的名称修饰未标准化,导出类的客户端将必须使用与您相同的编译器(通常是相同的编译器版本)——但这在封闭环境中可能是可以接受的。顺便说一句,这就是您在安装新软件之前通常必须安装 Microsoft 的 Visual C++ 200X Redistributable 软件包的原因。 MS 可再发行软件包专门针对 Windows 平台上的 CPP 运行时库解决了这个问题。
This is only true for C linkage (i.e. if you use extern "C" when exporting). There's no technical problem exporting a template class out of a shared library -- just think of the STL classes exported from the C++ runtime library.
It's true that due to CPP's name mangling not being standardized, the client of your exported class will have to use the same compiler you do (and often the same compiler version) -- but this may be acceptable in closed environments. BTW, this is the reason you often have to install Microsoft's Visual C++ 200X Redistributable packages before installing new software. MS redistributable packages solve this problem specifically for the CPP runtime libraries on Windows platforms.
导出模板的方法是有的,如果你使用的是Visual Studio,可以查看__declspec(dllimport/dllexport),功能非常强大。我不知道其他编译器是否提供这个。但是,如果您不导出 C 兼容接口,那么您基本上是在强制用户使用与您相同的编译器,至少是相同的供应商,如果不是相同的模型。
There are methods for exporting templates, if you're using Visual Studio, you can check out
__declspec(dllimport/dllexport)
, which is very powerful. I don't know if other compilers offer this. However, if you don't export a C-compatible interface, then you're basically forcing the user to use the same compiler as you, at least the same vendor, if not the same exact model.