是否劝阻过负载STL功能?

发布于 2025-01-21 07:07:05 字数 900 浏览 1 评论 0原文

我想知道是否不愿超载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 技术交流群。

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

发布评论

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

评论(2

睫毛溺水了 2025-01-28 07:07:06

否。只要您不试图将自定义超载放在命名空间std中,就没有问题,因为在某些情况下,这是未定义的。

考虑std ::交换甚至鼓励您在交换两个对象时提供超负荷的位置应执行std ::交换无法做的事情:

#include <iostream>
#include <algorithm>

namespace X {

struct foo {
    int value;
    bool operator<(const foo& other) { return value < other.value; }
};

void swap(foo& a, foo& b) noexcept {
    std::cout << "swap\n";
    std::swap(a.value,b.value);
}

}

int main() {
    std::vector<X::foo> v{{2},{1}};
    std::iter_swap(v.begin(),v.begin()+1);   // calls X::swap
    std::cout << v[0].value << " " << v[1].value << "\n";

    std::vector<int> w{2,1};
    std::iter_swap(w.begin(),w.begin()+1);  // calls std::swap
    std::cout << w[0] << " " << w[1];
}

输出

swap
1 2
1 2

这取决于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 that std::swap cannot do:

#include <iostream>
#include <algorithm>

namespace X {

struct foo {
    int value;
    bool operator<(const foo& other) { return value < other.value; }
};

void swap(foo& a, foo& b) noexcept {
    std::cout << "swap\n";
    std::swap(a.value,b.value);
}

}

int main() {
    std::vector<X::foo> v{{2},{1}};
    std::iter_swap(v.begin(),v.begin()+1);   // calls X::swap
    std::cout << v[0].value << " " << v[1].value << "\n";

    std::vector<int> w{2,1};
    std::iter_swap(w.begin(),w.begin()+1);  // calls std::swap
    std::cout << w[0] << " " << w[1];
}

Output:

swap
1 2
1 2

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 for int and the like.

说不完的你爱 2025-01-28 07:07:05

是否不建议使用STL功能?

您不允许在std名称空间中添加任何过载。将过载添加到自定义名称空间是可以的。

,如果是这样,为什么?

因为标准是这样说的。可能是为了使您的程序在标准(实施)更改时不会破坏。

Is it discouraged to overload STL functions?

You aren't allowed to add any overloads into the std namespace. It's fine to add overloads into custom namespaces.

and if so, why?

Because the standard says so. Probably so that your program won't break when the standard (implementation) changes.

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