简体   繁体   English

将std :: function与lambda和variadic模板一起使用

[英]Use std::function with lambda and variadic template

I found some nice loop function on internet, but wasn't able to make it my own. 我在互联网上找到了一些不错的循环功能,但是无法将其变成我自己的。 And the code was uncommented so that didn't help. 并且代码被取消注释,因此没有帮助。

Original code : 原始代码:

  template <typename T> struct identity { typedef T type; };

  template <typename ... Components>
  void each(typename identity<std::function<void(Base base, Components&...)>>::type f) {
    //do stuff
  }

The goal is to unpack some data from a class and apply the function on it, Foo there is the manager which contain the object, and this object has some int member inside.I hope it's clear enough. 目标是从类中解压缩一些数据并在其上应用函数,Foo有包含该对象的管理器,并且该对象内部有一些int成员。我希望它足够清楚。

I'd like to achieve something like that: 我想实现这样的目标:

#include <functional>
#include <iostream>

class Foo {

  template < typename ... T >
  void  for_each(std::function<void(T&...)> func) {
    std::cout << "for_each" << std::endl;
  }
};

int main() {

  Foo  test;
  test.for_each<int>([](int& data) {
    std::cout << "main" << std::endl;
  });
  return 0;
}

But I have this error instead: 但我有这个错误:

Test.cpp: In function ‘int main()’:
Test.cpp:17:4: error: no matching function for call to ‘Foo::for_each(main()::<lambda(int&)>)’
   });
    ^
Test.cpp:7:9: note: candidate: template<class ... T> void Foo::for_each(std::function<void(T& ...)>)
   void  for_each(std::function<void(T&...)> func) {
         ^~~~~~~~
Test.cpp:7:9: note:   template argument deduction/substitution failed:
Test.cpp:17:4: note:   ‘main()::<lambda(int&)>’ is not derived from ‘std::function<void(T& ...)>’
   });
    ^

Seems like there was some bugs with this code, I hope it's not the case anymore. 看起来这个代码有一些错误,我希望不再是这种情况了。 If you have some advice to make this code works, well you'll make a man happy. 如果你有一些建议让这个代码有效,那么你就会让一个男人开心。

If you specify a std::function as the argument type, then a std::function must be passed. 如果指定std::function作为参数类型,则必须传递std::function A lambda is not a std::function , but it can be used to create one. lambda不是std::function ,但它可以用来创建一个。

#include <functional>
#include <iostream>

class Foo {

  public:
  template < typename ... T >
  void  for_each(std::function<void(T&...)> func) {
    std::cout << "for_each" << std::endl;
  }
};

int main() {

  Foo  test;
  test.for_each<int>(std::function<void(int&)>([](int& data) {
    std::cout << "main" << std::endl;
  }));
  return 0;
}

This may not be what you want (at the moment, I can only guess at that), bu this edit at least allows the example to compile. 这可能不是你想要的(目前,我只能猜测),这个编辑至少允许这个例子进行编译。

Based on 基于

The goal is to unpack some data from a class and apply the function on it, Foo there is the manager which contain the object, and this object has some int member inside 目标是从类中解压缩一些数据并在其上应用函数,Foo有包含该对象的管理器,并且该对象内部有一些int成员

I think you are looking for something like: 我想你正在寻找类似的东西:

#include <functional>
#include <iostream>
#include <vector>

class Foo {

   public:

      template < typename T >
         void  for_each(std::function<void(T&)> func) {
            std::cout << "for_each" << std::endl;
            for ( int i : data)
            {
               func(i);
            }
         }

      std::vector<int> data;
};

int main() {

  Foo test;
  test.data = {1, 2, 3, 4, 5};

  test.for_each<int>([](int& data) {
    std::cout << "In main, data:" << data << std::endl;
  });
  return 0;
}

If you plan to use non-capturing lambdas only, you can use a function pointer as an argument and let the lambda decay to such a type. 如果您计划仅使用非捕获lambdas,则可以使用函数指针作为参数,并让lambda衰减为这种类型。
It follows a minimal, working example: 它遵循一个最小的工作示例:

#include <iostream>

class Foo {
public:
  template < typename ... T>
  void  for_each(void(*func)(T&...)) {
    std::cout << "for_each" << std::endl;
  }
};

int main() {
  Foo  test;
  test.for_each(+[](int& data) {
    std::cout << "main" << std::endl;
  });
  return 0;
}

If you can deal with a class template, another possible approach is the following one: 如果您可以处理类模板,另一种可能的方法是:

#include <functional>
#include <iostream>

template < typename ... T >
class Foo {
public:
  void  for_each(std::function<void(T&...)> func) {
    std::cout << "for_each" << std::endl;
  }
};

int main() {
  Foo<int> test;
  test.for_each([](int& data) {
    std::cout << "main" << std::endl;
  });
  return 0;
}

Note that, if you are not actually interested in the type T , you can use a generic type and get away with it: 请注意,如果您实际上对类型T不感兴趣,可以使用泛型类型并使用它:

#include <iostream>

class Foo {
public:
  template<typename F>
  void  for_each(F &&func) {
    std::cout << "for_each" << std::endl;
  }
};

int main() {
  Foo  test;
  test.for_each([](int& data) {
    std::cout << "main" << std::endl;
  });
  return 0;
}

It works with both capturing and non.capturing lambdas. 它适用于捕获和非捕获lambda。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM