这个卷曲支架CPP语法是什么?

发布于 2025-01-24 16:02:34 字数 1153 浏览 1 评论 0原文

las,很难搜索诸如括号之类的符号。

我遇到了此代码:

#include <functional>
#include <iostream>
 
template <typename A, typename B, typename C = std::less<>>
bool fun(A a, B b, C cmp = C{})
{
    return cmp(a, b);
}
 
int main()
{
    std::cout
      << std::boolalpha
      << fun(1, 2)   << ' ' // true
      << fun(1.0, 1) << ' ' // false
      << fun(1, 2.0) << ' ' // true
      << std::less<int>{}(5, 5.6)    << ' ' // false: 5 < 5 (warn: implicit conversion)
      << std::less<double>{}(5, 5.6) << ' ' // true: 5.0 < 5.6
      << std::less<int>{}(5.6, 5.7)  << ' ' // false: 5 < 5 (warn: implicit conversion)
      << std::less{}(5, 5.6)         << ' ' // true: less<void>: 5.0 < 5.6
      << '\n';
}

https://en.cppreference.com/w /CPP/实用程序/功能/少

语句中的牙套c {}std :: Less&lt&gt; {}(5,5.6)是指的是什么?

Alas, it's hard to google for symbols like braces.

I came across this code:

#include <functional>
#include <iostream>
 
template <typename A, typename B, typename C = std::less<>>
bool fun(A a, B b, C cmp = C{})
{
    return cmp(a, b);
}
 
int main()
{
    std::cout
      << std::boolalpha
      << fun(1, 2)   << ' ' // true
      << fun(1.0, 1) << ' ' // false
      << fun(1, 2.0) << ' ' // true
      << std::less<int>{}(5, 5.6)    << ' ' // false: 5 < 5 (warn: implicit conversion)
      << std::less<double>{}(5, 5.6) << ' ' // true: 5.0 < 5.6
      << std::less<int>{}(5.6, 5.7)  << ' ' // false: 5 < 5 (warn: implicit conversion)
      << std::less{}(5, 5.6)         << ' ' // true: less<void>: 5.0 < 5.6
      << '\n';
}

in https://en.cppreference.com/w/cpp/utility/functional/less.

What do braces in the statements C{}, std::less<int>{}(5, 5.6) mean?

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

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

发布评论

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

评论(1

水溶 2025-01-31 16:02:35

创建一个实例 std :: selly&lt&lt; int&lt; /code>

然后通过5,5.6作为bool Operator()(const int&amp;,const int&amp;)的参数


大致相同

auto temp = std::less<int>{};
std::cout << temp(5, 5.6);

it create an instance of std::less<int>

then pass 5,5.6 as the parameter to it's bool operator()(const int&, const int&)


it's roughly the same as

auto temp = std::less<int>{};
std::cout << temp(5, 5.6);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文