是否劝阻过负载STL功能?
我想知道是否不愿超载STL功能,如果是,为什么?
我昨天遇到了一个问题,发现std :: fpClassify
在Microsoft Compilers上没有积分超载( https://learn.microsoft.com/en-us/cpp/cpp/cpp/cpp/c-runtime-library/reference /fpClassify?view = msvc-170 )与其他编译器一样(请参见 https://en.cppreference.com/w/cpp/numeric/math/math/fpclassify )。
编译时,我遇到了这个问题
T var; // T can be an integral type
std::isnan(var); //
当我尝试使用Microsoft C ++编译器
。我已经有一个工作解决方案来解决此问题,该问题不涉及过载std :: fpClassify
,但是我确实考虑了可能只是为std :: fpclassify
我自己写一个过载。 ,但是看来这本来会变得有毛,因为代码可以使用非Microsoft编译器编译,在这种情况下,我们已经定义了积分超载。
I was wondering if it is discouraged to overload an STL function, and if so, why?
I ran across an issue yesterday, where I found that std::fpclassify
doesn't have an integral overload on microsoft compilers (https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fpclassify?view=msvc-170) as it does for other compilers (see (4) in https://en.cppreference.com/w/cpp/numeric/math/fpclassify).
I ran across this issue when I tried to compile
T var; // T can be an integral type
std::isnan(var); //
using microsoft C++ compiler.
I already have a working solution to solve this issue that didn't involve overloading std::fpclassify
, but I did consider maybe just writing an overload for std::fpclassify
myself, but it seems it would have gotten hairy because the code might be compiled using non-microsoft compilers, in which case, we would already have the integral overload defined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
否。只要您不试图将自定义超载放在命名空间
std
中,就没有问题,因为在某些情况下,这是未定义的。考虑
std ::交换
甚至鼓励您在交换两个对象时提供超负荷的位置应执行std ::交换
无法做的事情:输出:
这取决于ADL,这意味着它对
std
的基本类型或类型都无法使用。仅当您将它们包装在自定义类中的某个名称空间中时,就可以将其用于int
等。No. There is no issue as long as you are not trying to place your custom overload inside the namespace
std
, because that is undefined except in some cases.Consider
std::swap
where you are even encouraged to provide an overload when swapping two objects should do something thatstd::swap
cannot do:Output:
This relies on ADL, which means it wont work for fundamental types or types from
std
. Only if you wrap them inside a custom class inside some namespace like above you can use it forint
and the like.您不允许在
std
名称空间中添加任何过载。将过载添加到自定义名称空间是可以的。因为标准是这样说的。可能是为了使您的程序在标准(实施)更改时不会破坏。
You aren't allowed to add any overloads into the
std
namespace. It's fine to add overloads into custom namespaces.Because the standard says so. Probably so that your program won't break when the standard (implementation) changes.