文件本地定义

发布于 2024-09-11 11:26:57 字数 745 浏览 2 评论 0原文

这个问题更多地属于最佳实践和干净/安全的分发代码的范畴。

我正在为我的作品集开发一个 C++ 数学库,并在大学最后两个学期使用。我希望这个库非常易于使用,并最大限度地减少与现有代码发生冲突的可能性。

为了便于阅读,我将 TEMP_T 定义为类的模板,位于每个头文件(数学/矩阵/vec/四元数)的顶部。它看起来如下所示:

#ifdef TEMP_T
#define UNDEF_TEMP_T TEMP_T // Used to reset any other definitions later.
#endif // TEMP_T
#define TEMP_T  template<class T> // Used to make the code more readable.

稍后,在文件末尾,如果需要的话,我会重置预先存在的定义:

#undef TEMP_T // Get rid of our definition.
#ifdef UNDEF_TEMP_T
#define TEMP_T UNDEF_TEMP_T // Reset the previous definition, if it existed.
#undef UNDEF_TEMP_T
#endif // UNDEF_TEMP_T

我的问题:这会成功创建一个对文件可见的定义吗?和文件本身?如果是这样,您会采用这种方式来完成这样的事情吗?如果没有,您能告诉我一些您做事方式背后的理性吗?

This question more falls into the category of best practices, and clean/safe code for distribution.

I'm working on a math library in C++, for my portfolio, and to use during my last two semesters of College. I want this library to be very easy to use, and to minimize the possibilities of conflicts with pre existing code.

For readability I'm defining TEMP_T as a template for a class, at the top of each of my header files (math/matrix/vec/quaternion). It looks like the following:

#ifdef TEMP_T
#define UNDEF_TEMP_T TEMP_T // Used to reset any other definitions later.
#endif // TEMP_T
#define TEMP_T  template<class T> // Used to make the code more readable.

Later on, at the end of the file, I reset the pre existing definition, if nessicary with the following:

#undef TEMP_T // Get rid of our definition.
#ifdef UNDEF_TEMP_T
#define TEMP_T UNDEF_TEMP_T // Reset the previous definition, if it existed.
#undef UNDEF_TEMP_T
#endif // UNDEF_TEMP_T

My question: Would this successfully create a define visible to the file, and the file alone? If so, is this how you would go about accomplishing such a thing? If not, would you be so kind as to give me some insight on your rational behind your ways of doing things?

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

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

发布评论

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

评论(5

末が日狂欢 2024-09-18 11:26:57

IMO 的可读性要差得多,属于预处理器滥用的类别,我强烈建议使用实际的定义,这将使您的代码更容易被其他人阅读,这就是可读性。

IMO that is much less readable, falls into the class of pre-processor abuse and I would seriously recommend using the actual definition which will make your code more readable by others which is the point of readability.

岁月流歌 2024-09-18 11:26:57

在我看来,这样会降低可读性。这将是减少要输入/读取的字符数量的方法,但它也增加了所需的人工解释的数量,这是一件坏事。

最重要的是,如果有人在自己的代码中定义了 TEMP_T,他们会通过包含您的标头来失去其定义。

// my code.cpp
#define TEMP_T( var, vartype ) myclass<vartype> var( ##var );

#include <yourmathlib.h>
TEMP_T( anint, int )  // breaks.  hard to find the real error.

因此,您可以定义特定于库的 MYMATHLIB_TEMP_T,这进一步降低了可读性:)

In my eyes, you reduce readability this way. This would be the way to reduce the number of characters to be typed/read, but it increases the number of needed human-interpretations, too, which is a bad thing.

On top of that, if anyone has defined TEMP_T in their own code, they would loose their definition by including your headers.

// my code.cpp
#define TEMP_T( var, vartype ) myclass<vartype> var( ##var );

#include <yourmathlib.h>
TEMP_T( anint, int )  // breaks.  hard to find the real error.

As a consequence you may define a library-specific MYMATHLIB_TEMP_T, which further decreases readability :)

虚拟世界 2024-09-18 11:26:57

您只需#include 从其他头文件中声明模板的头文件即可。

如果您想确保不会从两个不同的文件中两次包含相同的文件,则可以使用

#ifndef MYMATHLIB_TEMP_T
#define MYMATHLIB_TEMP_T

// ... template ...

#endif

模板头文件。

You can just #include the header file where you declared the template from your other header files.

And if you want to make sure that you don't include the same file twice from 2 different files, you can have

#ifndef MYMATHLIB_TEMP_T
#define MYMATHLIB_TEMP_T

// ... template ...

#endif

around the template header file.

栀梦 2024-09-18 11:26:57

您可以在头文件中定义 TEMP_T 并在所有代码中使用该标头,也许您可​​以尝试测试您的标头是否包含在某个宏中。
但这种做法会增加阅读代码的复杂性。

you could just define TEMP_T in an header file and use that header in all your code, maybe you can try to test if your header is included by some macro.
But this way of doing things increases the complexity to read your code.

幽蝶幻影 2024-09-18 11:26:57

为什么不简单地将模板类放在命名空间中呢?如果你把它放在一个命名空间中,你就可以避免所有讨厌的宏东西。命名空间被明确设计为允许代码单元彼此隔离而不会发生命名冲突。

Why not simply put your template class in a namespace? If you stick it in an namespace, you avoid all the nasty macro stuff. namespaces were explicitly designed to allow code units to be isolated form each other without naming conflicts.

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