标签: std-function

我试图传递一个lambda作为参数

试图将lambda传递给构造函数:

#include <functional>
#include <exception> 

template<typename R>
class Nisse
{
    private:
        Nisse(Nisse const&)             = delete;
        Nisse(Nisse&&)                  = delete;
        Nisse& operator=(Nisse const&)  = delete;
        Nisse& operator=(Nisse&&)       = delete;
    public:
        Nisse(std::function<R> const& func) {}
};

int main()
{
    Nisse<int>   nisse([](){return 5;});
}
Run Code Online (Sandbox Code Playgroud)

当我编译时,我收到一条错误消息:

Test.cpp: In function ‘int main()’:
Test.cpp:19:39: error: no matching function for call to ‘Nisse<int>::Nisse(main()::<lambda()>)’
Test.cpp:19:39: note: candidate is:
Test.cpp:14:9: note: Nisse<R>::Nisse(const std::function<R>&) [with R = int]
Test.cpp:14:9: note:   no known conversion for argument 1 from ‘main()::<lambda()>’ to ‘const std::function<int>&’
Run Code Online (Sandbox Code Playgroud)

c++ lambda c++11 std-function

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

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

我通常从不写 C++,今天我尝试尝试 C++ 模板。我实现了一个看起来像这样的 Maybe 类型

#include <functional>
#include <iostream>
#include <string>
template<typename T>
class TMaybe
{
  T value;
public:
  TMaybe() : value(nullptr){}
  TMaybe(T &&v) : value(v){}
  TMaybe(T v) : value(v){}
};

template<typename T, typename R>
TMaybe<R> maybe_if(const TMaybe<T> &m, std::function<R(T v)> f){
  return (m.value != nullptr) ? TMaybe<R>(f(m)) : TMaybe();
}

int main(){
  int i = 10;
  auto m = TMaybe<int>(i);
  auto plus_ten = [](int i) -> int {return i + 10;};
  maybe_if(m, plus_ten); // could not deduce template …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11 std-function

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

制作一个指向两个函数c ++的std :: funtion

如果我有两个功能

void foo()
{
    std::cout << 1 << std::endl;
}

void bar()
{
    std::cout << 2 << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我有一个函数指针

std::function<void()> v;
Run Code Online (Sandbox Code Playgroud)

我想要v()打印

1
2
Run Code Online (Sandbox Code Playgroud)

c++ std-function

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

模板参数类型推导在函数对象中不起作用

我有一个模板函数,带有模板化参数类型的另一个函数.

使用它时,我必须明确指定参数类型(1)否则不会编译(2).

template<typename T>
void process(const std::function<void(const T &)>& f)
{
   // ...
}

process<Foo>( [&](const Foo& arg){/*...*/} ); // (1) Ok!

// process( [&](const Foo& arg){/*...*/} ); // (2) Won't Work! 
Run Code Online (Sandbox Code Playgroud)

让(2)工作有什么诀窍吗?

c++ templates std-function type-deduction

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

带有指向函数的指针的 C++ 向量 push_back

我有一类Foo含有vectorBarS:

class Foo
{
public:
    void create();
    void callback();

    std::vector<Bar> mBars;
}
Run Code Online (Sandbox Code Playgroud)

我的Bar类包含这个构造函数:

class Bar
{
    Bar(const int x, const int y, std::function<void()> &callback);
}
Run Code Online (Sandbox Code Playgroud)

我的Foo班级有一个create()Bars添加到mBars向量的方法:

void Foo::create()
{
    mBars.push_back({ 1224, 26, callback }); //ERROR!
}
Run Code Online (Sandbox Code Playgroud)

如何设置函数指针,使用std::function?并且还没有创建单独的对象并push_back进入向量?就像上面那行,我得到错误的地方:

E0304   no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=CV::Button, _Alloc=std::allocator<Bar>]" matches the argument list    
Run Code Online (Sandbox Code Playgroud)

c++ function-pointers vector std-function

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

std :: function和std :: function的比较

我有以下功能

typedef std::function<void(const data<3> & data, void * userData )> callbackFnc;
Run Code Online (Sandbox Code Playgroud)

和一些结构

typedef struct
{
  callbackFnc callback;
  void * userData;
} callbackData;
Run Code Online (Sandbox Code Playgroud)

我将所有这些存储在我的班级中

std::vector<std::shared_ptr<callbackData> > mCallbacks;
Run Code Online (Sandbox Code Playgroud)

这是我添加的方式:

bool MyClass::addCallback(callbackFnc cbFunc, void * userData)
{
std::shared_ptr<callbackData> cb = std::make_shared<callbackData>();
    cb->callback = cbFunc;
    cb->userData = userData;

    mCallbacks.push_back(cb);
}
Run Code Online (Sandbox Code Playgroud)

现在,我要检查是否已添加给定功能并将其删除。为此,我只需要比较函数的地址,如下所示:

for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++)
{
    // Compare address of these two functions
    if ((&(*it)->callback) == &cbFunc)
    {
        mCallbacks.erase(it);

        result = true;
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

看起来这种比较是不正确的,但我不知道为什么。任何建议都会有用。

谢谢!

c++ std-function

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

将 lambda 参数传递给没有中间变量的 std::function 参数

这可能看起来类似于"I cannot pass lambda as std::function",但我实际上是std::function按值传递参数,因此该问题不适用。我定义了以下函数。

template<typename T>
std::vector<T> countSort(const std::vector<T> &v, std::function<int(T)> keyFunc, int n);
Run Code Online (Sandbox Code Playgroud)

第二个参数是std::function映射Tint(按值传递)的 。

在调用这个时,我想使用一个 lambda 表达式,如下:

std::vector<int> v;
[...]
v = countSort(v, [](int x) { return x; }, 10);
Run Code Online (Sandbox Code Playgroud)

但是模板参数推导失败,因为“main()::<lambda(int)>不是从std::function<int(T)>”派生的。如果我指定模板参数,或者如果我std::function为 lambda 表达式引入一个类型的中间变量,它确实有效:

std::function<int(int)> lambda = [](int x) { return x; };
v = countSort(v, lambda, 10);
Run Code Online (Sandbox Code Playgroud)

为什么我不能做前者?我给编译器完全相同的信息;如果它能够类型的值转换lambda<int>,以std::function<int(int)>将其分配给一个变量时,为什么不能直接从转换lambda<int>参数类型,这是std::function<T(int)>-和考虑到v的类型的std::vector<int>,应该知道 …

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

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

std::function 与可作为模板参数调用

在下面的示例中,为什么第 20 行会导致第 27 行到第 30 行描述的错误?

在第 33 行调用exec1效果很好。

#include <cstdint>
#include <functional>
#include <iostream>
#include <tuple>
#include <type_traits>

template <typename... t_fields>
void exec0(std::function<std::tuple<t_fields...>()> generate,
           std::function<void(t_fields &&...)> handle) {
  std::tuple<t_fields...> _tuple{generate()};
  std::apply(handle, std::move(_tuple));
}

template <typename t_generate, typename t_function>
void exec1(t_generate generate, t_function handle) {
  auto _tuple{generate()};
  std::apply(handle, std::move(_tuple));
}

int main() {
  auto _h = [](uint32_t u) -> void { std::cout << "u = " << u << '\n'; };

  auto _g = []() -> std::tuple<uint32_t> …
Run Code Online (Sandbox Code Playgroud)

c++ variadic-templates std-function template-argument-deduction stdapply

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

如何将 std::function 的引用值从一个类传递到另一个类

我有以下场景。

File Box.h
#pragma once
#include <functional>

class Box
{
    public:
        Box() :m_data(45) {}
        void set_callback(std::function<int(int, int& c)> cb)
        {
            f = cb;
        }
        auto get_box_dimension(void)
        {
             return f(4,m_data);
        }
    

private:
    std::function<int(int, int&)> f;
    int m_data;
};

File SquareBox.h
#pragma once
#include <functional>

class SquareBox
{
public:
    int sq_func(int a, int& b)
    {
        return a * b;
    }

private:


};

File main.cpp
#include<iostream>
#include<functional>
#include"Box.h"
#include"SquareBox.h"

int main()
{
    int x = 1;
    int a = 5;
    Box b;
    SquareBox …
Run Code Online (Sandbox Code Playgroud)

c++ std-function

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

std::function 的参数列表如何工作

如果这是一个非常菜鸟的问题,我很抱歉,但我对如何指定 std::function 参数列表感到困惑,如下所示:

std::function<void(double)> func;
Run Code Online (Sandbox Code Playgroud)

但在实际的函数中,这是行不通的:

void passFunc(void(double) func) {}
Run Code Online (Sandbox Code Playgroud)

对于上面的方法,您必须指定一个函数指针。那么如何允许 void(double) 传递到 std::function 中?void(double) 是一种类型吗?如果不是,它怎么会出现在 std::function 的参数列表中?如果它是类型,为什么 void(double) 作为函数参数的类型是无效的?

c++ templates parameter-passing std-function

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