如何简化C++0x中繁琐的lambda参数声明?

发布于 2024-10-05 05:03:35 字数 558 浏览 3 评论 0原文

最简单的代码是最好的提问者:

#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector<int> coll;
    for_each(coll.begin(), coll.end(), [](vector<int>::value_type n) -> void {});

    return 0;
}

这里,vector::value_type n 很乏味。我想要一个类似自动的实用程序来自动推导 n 的正确类型;就像下面这样:

for_each(coll.begin(), coll.end(), [](auto_type n) -> void {});

为了更贪婪,我希望 auto_type 接受一个用于推导 n 的正确类型的参数。参数可以是对容器的(智能)指针或引用,或者容器的迭代器。

各位高手,请问如何实现呢?

The simplest code is the best asker:

#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector<int> coll;
    for_each(coll.begin(), coll.end(), [](vector<int>::value_type n) -> void {});

    return 0;
}

Here, vector<int>::value_type n is tedious. I want to have an auto-like utility to deduce the right type of n automatically; just like the following:

for_each(coll.begin(), coll.end(), [](auto_type n) -> void {});

To be more greedy, I want auto_type to take an argument used to deduce the right type of n. The argument can be a (smart) pointer or reference to the container, or an iterator of the container.

Dear gurus, how to implement that?

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

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

发布评论

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

评论(3

故乡的云 2024-10-12 05:03:35

您不必在该函数中声明 void return。您可以使用 decltype,例如 decltype(coll[0])

std::for_each(coll.begin(), coll.end(), [](decltype(coll[0]) value) {
    std::cout << value;
});

编辑:

template<typename T> auto type(T&& t) -> decltype(*std::forward<T>(t).begin()) {
    return *t.begin();
}
std::for_each(coll.begin(), coll.end(), [](decltype(type(coll)) value) {
});

You don't have to declare the void return in that function. You could use decltype, like, decltype(coll[0]).

std::for_each(coll.begin(), coll.end(), [](decltype(coll[0]) value) {
    std::cout << value;
});

Edit:

template<typename T> auto type(T&& t) -> decltype(*std::forward<T>(t).begin()) {
    return *t.begin();
}
std::for_each(coll.begin(), coll.end(), [](decltype(type(coll)) value) {
});
梦里南柯 2024-10-12 05:03:35

您永远不应该写 vector::value_type 而不是 int,因为您已经知道它们是相同的。一个更好的例子会有所帮助;我还想要更简单的 lambda 参数。

然而,推导参数类型取决于 lambda 的使用方式,并且只能通过了解 for_each 的详细信息才能知道。但是函数重载解析取决于了解参数的类型,因此参数类型不能取决于函数如何使用它,否则就会出现循环依赖。

在当前的 C++ 中,您可以通过将函子类型与其参数类型解耦来避免这种循环依赖:

struct AddExample {
  template<class T>
  T operator()(T a, T b) {
    return a + b;
  }
};

some_algo(begin, end, AddExample());

可以对 lambda 语法进行等效操作,但代价是修改一些语义(例如,隐式转换为函数指针和 std::function),但我不认为 C++0x 会发生这种情况。

You should never write vector<int>::value_type instead of int, as you already know those are identical. A better example would help; I also would like simpler lambda parameters.

However, deducing parameter type depends on how the lambda is used, and that can only be known by knowing details of for_each. But function overload resolution depends on knowing the types of parameters, so parameter type cannot depend on how the function uses it, or you'd have a circular dependency.

In current C++, you avoid this circular dependency by decoupling the functor type from its parameter types:

struct AddExample {
  template<class T>
  T operator()(T a, T b) {
    return a + b;
  }
};

some_algo(begin, end, AddExample());

The equivalent could be done for the lambda syntax, at the cost of munging some semantics (e.g. implicit conversion to function pointers and std::function), but I don't see it happening for C++0x.

冰雪之触 2024-10-12 05:03:35

lambda 提案的早期版本包括所谓的多态 lambda 的规定,其简单语法如下:

auto print = [](x) { std::cout << x; };
print(42);
print("foo");

不幸的是,该提案中存在一些技术问题,委员会认为鉴于日程已经非常紧张,无法得到令人满意的解决,因此多态 lambda 的引入被推迟到未来的标准修订版,而不是标准化可能损坏的功能。

The early versions of the lambda proposal included provision for so-called polymorphic lambdas, with a simple syntax as follows:

auto print = [](x) { std::cout << x; };
print(42);
print("foo");

Unfortunately, there were technical issues in the proposal that the committee felt could not have been satisfactorily resolved given the already very tight schedule, so instead of standardizing a possibly broken feature, the introduction of polymorphic lambdas was deferred to a future standard revision.

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