我可以在 STL 中禁用 min、max 吗?
我使用一个非常重要且不可触及的库。 问题是库声明了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
min 和 max 函数是在
std
命名空间中定义的,因此此代码不应编译:如果可以编译,则您的标准库不兼容。此外,您的重要且不可触及的库应该在名称空间中声明它的函数。如果不是的话。向卖家大声抱怨。
编辑:由于这些函数可能位于头文件中,因此您可以触摸它们。因此,一种破解方法是从标头中删除模板并将其替换为以下内容:
尽管任何人都在猜测库的作者为何认为需要定义这些模板。
The min and max functions are defined in the
std
namespace, so this code should not compile: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:
though why the writers of the library felt the need to define these templates is anyone's guess.
由于
min
和max
(以及所有其他标准库成员)都是在std
命名空间中定义的,因此您不能导入该命名空间,即不要使用using namespace std;
。您仍然可以通过显式命名空间结果来使用 STL,例如。std::max
、std::cout
等。As both
min
andmax
(and every other standard library member) are defined instd
namespace, you just mustn't import that namespace, i.e. don't useusing namespace std;
. You can still use STL, by explicit namespace resultion, eg.std::max
,std::cout
etc.我有时也会遇到类似的问题,因为 iirc OpenCV #定义了它自己的最小值/最大值,我认为 windows.h 也是如此。
不过,通常我会简单地通过 Neil 是对的来帮助自己
,因为 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
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()
如果您的不可触及的库不在命名空间中,您可以强制查找使用全局范围,而不是 std:
更好的解决方案是删除
您的库并将其放入命名空间中。
If your untouchable library is not in a namespace, you could force the lookup to use global scope, rather than std:
A better solution would be to remove
and get your library in a namespace.
定义
NOMINMAX
,您将不会获得这些函数中的任何一个。Define
NOMINMAX
, and you won't get either of these functions.我猜你的库将 min/max 定义为宏,因此 STL min/min 位于命名空间中这一事实无济于事。您可以尝试这样做:
这样,在编译算法时,最小值和最大值会被转换为其他内容。当然,只有当您在库之前包含算法时,这才有效。
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:
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.