标签: std-function

std :: functions和lambda函数传递

我有一个类std::function作为参数,我分配一个lambda函数.它在构造函数中工作,但之后停止工作.f运行第一行后调试器显示为"空".为什么?

#include <iostream>
#include <string>
#include <functional>

typedef std::function<void(std::string)> const& fn;

class TestClass
{
public:
    TestClass(fn _f) : f(_f) { F(); }
    void F() { f("hello"); };

private:
    fn f;
};


int main()
{
    TestClass t([](std::string str) {std::cout << str << std::endl; });

    t.F();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

呼叫t.F()导致故障.为什么?

我可以通过将其更改为以下内容来解决此问题:

int main()
{
    fn __f = [](std::string str) {std::cout << str << std::endl; };
    TestClass t(__f);

    t.F();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但同样,这并不当我改变工作fn来auto! …

c++ lambda c++11 std-function

4
推荐指数
1
解决办法
1173
查看次数

使用 std::function 或转发引用作为高阶函数的通用可调用对象输入参数?

我想知道编写一个高阶函数的主要区别、优缺点,作为输入参数,astd::function或转发引用,例如template<typename F> void hof(F&& fun);. 显然,前者比后者更严格,因为它指定了输入可调用对象必须符合的函数类型。

c++ perfect-forwarding c++11 std-function

4
推荐指数
1
解决办法
273
查看次数

将 void* 转换为 std::function

我有一个问题。我正在尝试将 void* 转换为 std::function 。这只是一个简单的例子,任何建议将不胜感激

#.h file
class Example {

public:

  Example();
  int foo(void* hi);

  int fooFunc(std::function<int(int, int)> const& arg, int x, int y) {
    foo(arg.target<void*>(), x, y);
    return 2;
  }

};


#.cpp file
Example::Example() {

  }

  int Example::foo(void * func, int x, int y)
  {
    //cast back to std::function
    func(x, y);
    std::cout << "running in foo: " << a << "\n";
    return a;
  }
Run Code Online (Sandbox Code Playgroud)

我尝试的每一次选角都不起作用。

我知道我可以在这个例子中发送一个std::function,但这是为了更大的事情,我正在研究一个例子以使其在这里工作。

,的全部含义void*是有时在这些情况下使用它,当您不知道您将收到什么时,然后将其转换为您需要的特定用法。

谢谢!

casting void-pointers c++11 std-function

4
推荐指数
1
解决办法
5634
查看次数

类内的 std::function 初始化

我试图了解 std::function 是如何工作的,但我无法编译它,也不明白为什么。我认为这与在类中使用 std::function 有关系,因为没有类(在全局范围内定义的映射)它可以工作。

这是我收到的错误消息:

functor.cc:37:9:错误:无法转换‘{{"A", ((C*)this)->C::f}, {"B", ((C*)this)->C::g}, {"C", ((C*)this)->C::h}}’从 ‘<brace-enclosed initializer list>’到 ‘std::map<std::__cxx11::basic_string<char>, std::function<bool(const std::vector<std::__cxx11::basic_string<char> >&)> >’

示例代码(它没有意义,但它很好地代表了我遇到的问题):

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


class C { 
    public:
        bool f(const std::vector<std::string>& s) {
            std::cout << "F" << std::endl;
            for (auto& i : s) {
                std::cout << i << std::endl;
            }
            return true;
        }

        bool g(const std::vector<std::string>& s) {
            std::cout << "G" << std::endl;
            for (auto& i : s) {
                std::cout << i …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 std-function

4
推荐指数
1
解决办法
1393
查看次数

无法推导出std :: function模板参数

我正在研究类似下面的代码

#include <functional>

template <typename Type>
void foo(const std::function<void(const Type&)> & handler) {}

void goo (const int&){}

int main() {
    foo([](const int&){});
    foo(goo);
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,由于跟随错误,它拒绝编译(clang 6.0.0和gcc 8.1.1)

candidate template ignored: could not match 'function<void (const type-parameter-0-0 &)>' against '(lambda at test3.cpp:13:9)'
candidate template ignored: could not match 'function<void (const type-parameter-0-0 &)>' against '(lambda at test3.cpp:13:9)'
Run Code Online (Sandbox Code Playgroud)

有可能以某种方式迫使它Type正确演绎吗?

c++ templates std-function template-argument-deduction c++17

4
推荐指数
1
解决办法
217
查看次数

具有不同返回类型的函数变体

下面的代码不能编译:

#include <functional>
#include <variant>

int main() {
  using ret_void = std::function<void()>;
  using ret_int  = std::function<int()>;

  std::variant<ret_void, ret_int> var;
  var.emplace([](){ return 1; } );
}
Run Code Online (Sandbox Code Playgroud)

编译说template argument deduction/substitution failed。谁能解释为什么这无法编译?

c++ variant std-function

4
推荐指数
1
解决办法
93
查看次数

请求从 lambda 到非标量类型的转换

我创建了这个类,这样我就可以拥有任何类型的值,该值要么是固定的,要么是在每次使用时重新计算:

template<typename T>
class Value {
    private:
    bool fixed;
    union {
        T value;
        std::function<T()> get;
    };
    public:
    Value(const T& value) : fixed(true), value(value) {}
    Value(const std::function<T()>& get) : fixed(false), get(get) {}
    Value(const T *pointer) : Value([pointer]() { return *pointer; }) {}
    ~Value() {}
    operator T() { return fixed ? value : get(); }
};
Run Code Online (Sandbox Code Playgroud)

以下所有表达式似乎都可以正常工作:

Value<double> a = 2.2;
double b = 1.;
double c = a;
Value<double> d = &b;
Value<int> e = Value<int>([]() { return 1.; });
Run Code Online (Sandbox Code Playgroud)

但是当我尝试这样做时: …

c++ lambda templates std-function

4
推荐指数
1
解决办法
1271
查看次数

为什么 std::function 没有进行类型检查?

#include <functional>

void toggleOk(bool& b) { b = !b; }
void toggleBroken(bool b) { b = !b; }
void toggleInt(int i) { i = !i; }
void tooManyParams(bool b, int i) { i = !b; }

int main()
{
    typedef std::function<void(bool&)> CallbackType;
    typedef std::function<void(bool)> WrongCallbackType;

    CallbackType cb1 = [](bool b) { b = !b; }; // Should throw error - missing reference
    CallbackType cb2 = toggleOk; // Ok

    CallbackType cb3 = toggleBroken; // Should throw error - missing reference
    CallbackType …
Run Code Online (Sandbox Code Playgroud)

c++ function c++11 std-function c++17

4
推荐指数
1
解决办法
87
查看次数

如何使这些std :: function参数明确无误?

传递lambda时,以下函数重载是不明确的.我发现std::function可以从大多数可调用类型构建,即使它们的签名不匹配.所以编译器无法分辨使用哪个函数.

template <typename T> void each(std::function<void(T)> iterator);
template <typename T> void each(std::function<void(T, id)> iterator);
template <typename T> void each(std::function<void(T&)> iterator);
template <typename T> void each(std::function<void(T&, id)> iterator);
Run Code Online (Sandbox Code Playgroud)

这里有一些类似的问题,但没有一个能解决我的问题.如何在不改变用法的情况下解决歧义?更重要的是,当时我必须明确提到模板类型.有没有解决的办法?

c++ lambda templates c++11 std-function

3
推荐指数
1
解决办法
474
查看次数

如何用std :: function和std :: ref替换std :: ptr_fun

我正在尝试学习C++ 11的新功能,我有这个代码:

void print(int t, string separator)
{
    cout << t << separator;
}


int elements[] = { 10, 20, 30, 40, 50, 60, 40 };
string delim = " - ";

for_each(elements, elements + 7, bind2nd(ptr_fun(print), delim));
Run Code Online (Sandbox Code Playgroud)

输出:

10 - 20 - 30 - 40 - 50 - 60 - 40 -
Run Code Online (Sandbox Code Playgroud)

关于ptr_fun,这个网站说:

从C++ 11开始,不推荐使用此函数和相关类型,而使用更通用的std :: function和std :: ref,这两者都从普通函数创建可调用的适配器兼容函数对象.

有人可以在没有ptr_fun和C++ 11推荐的函数的情况下重写上面的例子吗?

谢谢

c++ c++11 std-function

3
推荐指数
1
解决办法
533
查看次数