我可以在 STL 中禁用 min、max 吗?

发布于 2024-09-04 16:34:35 字数 298 浏览 3 评论 0原文

我使用一个非常重要且不可触及的库。 问题是库声明了 min、max 函数, 所以当我在项目中包含 STL 标头时,它们会发生冲突。 如果可以的话,我想禁用 STL 中的 min、max 函数(如 #define NOMNMAX)。 如果我不能,有什么解决办法?

重要的 : 抱歉,这不是宏。有两个函数是模板函数。

提前谢谢

template<T>
T min(const T& a, const T& b) { a < b ? a : b; }

I use one library which is very important and untouchable.
The problem is that library declare min, max function,
so when I include STL header in project, they conflict.
I want to disable min, max function in STL (like #define NOMNMAX) if I could.
If I can't, what would be solution?

Important :
Sorry, It's not Macro. Two functions are template functions.

like

template<T>
T min(const T& a, const T& b) { a < b ? a : b; }

Thanks, in advance.

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

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

发布评论

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

评论(6

简单气质女生网名 2024-09-11 16:34:35

min 和 max 函数是在 std 命名空间中定义的,因此此代码不应编译:

#include <algorithm>

int main() {
    int n = min( 1, 2 );
}

如果可以编译,则您的标准库不兼容。此外,您的重要且不可触及的库应该在名称空间中声明它的函数。如果不是的话。向卖家大声抱怨。

编辑:由于这些函数可能位于头文件中,因此您可以触摸它们。因此,一种破解方法是从标头中删除模板并将其替换为以下内容:

using std::min;
using std::max;

尽管任何人都在猜测库的作者为何认为需要定义这些模板。

The min and max functions are defined in the std namespace, so this code should not compile:

#include <algorithm>

int main() {
    int n = min( 1, 2 );
}

If it does, your Standard Library is non-compliant. Also, your important and untouchable library should be declaring it's functions in a namespace. If it isn't. complain loudly to the vendor.

Edit: As these functions are presumably in a header file, you can touch them. So a hack would be to remove the templates from the header and replace them with the following:

using std::min;
using std::max;

though why the writers of the library felt the need to define these templates is anyone's guess.

再浓的妆也掩不了殇 2024-09-11 16:34:35

由于 minmax (以及所有其他标准库成员)都是在 std 命名空间中定义的,因此您不能导入该命名空间,即不要使用using namespace std;。您仍然可以通过显式命名空间结果来使用 STL,例如。 std::maxstd::cout 等。

As both min and max (and every other standard library member) are defined in std namespace, you just mustn't import that namespace, i.e. don't use using namespace std;. You can still use STL, by explicit namespace resultion, eg. std::max, std::cout etc.

赏烟花じ飞满天 2024-09-11 16:34:35

我有时也会遇到类似的问题,因为 iirc OpenCV #定义了它自己的最小值/最大值,我认为 windows.h 也是如此。
不过,通常我会简单地通过 Neil 是对的来帮助自己

#undef min
#undef max

,因为 min 位于 std:: 命名空间中。对我来说,问题出现了,对于一些标头#defining min/max,我什至不能使用 std::min()

I sometimes have problems with something like that as well, because iirc OpenCV #defines its own min/max, as well as i think does windows.h.
Usually I help myself by simply

#undef min
#undef max

Neil is right, though, as min is in std:: namespace. For me the problem arises, that with some headers #defining min/max, i can't even use std::min()

不甘平庸 2024-09-11 16:34:35

如果您的不可触及的库不在命名空间中,您可以强制查找使用全局范围,而不是 std:

int i = ::min<int>(1,2);

更好的解决方案是删除

using namespace std

您的库并将其放入命名空间中。

If your untouchable library is not in a namespace, you could force the lookup to use global scope, rather than std:

int i = ::min<int>(1,2);

A better solution would be to remove

using namespace std

and get your library in a namespace.

猥琐帝 2024-09-11 16:34:35

定义NOMINMAX,您将不会获得这些函数中的任何一个。

#define NOMINMAX

Define NOMINMAX, and you won't get either of these functions.

#define NOMINMAX
寄意 2024-09-11 16:34:35

我猜你的库将 min/max 定义为宏,因此 STL min/min 位于命名空间中这一事实无济于事。您可以尝试这样做:

#define max STL_MAX
#define min STL_MIN
#include <algorithm>
#undef min
#undef max

这样,在编译算法时,最小值和最大值会被转换为其他内容。当然,只有当您在库之前包含算法时,这才有效。

I'm guessing your libary defines min/max as macros, so the fact that STL min/min are in a namespace won't help. You could try this:

#define max STL_MAX
#define min STL_MIN
#include <algorithm>
#undef min
#undef max

That way, min and max are translated to something else while algorithm is compiled. Of course this only works if you include algorithm before your library.

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