标签: std-function

将std :: function作为参数传递时出现问题

我有这个奇怪的问题(我在这里简化了代码)和clang 3.1(gcc工作正常).是不正当使用std :: function(通过值传递)还是clang bug?

template <typename Container>
struct A
{
  using function_type = std::function<char(char)>;

  A(Container c, function_type f) : it_(c.begin()), f_(f) {}
  typename Container::value_type operator*() { return *it_; }

  typename Container::iterator it_;
  function_type f_;
};

template <typename Cont>
A<Cont> makeA(Cont c, std::function<char(char)> f)
{
  return A<Cont>(c, f);
}

char f(char ch) { return ch; }

int main()
{
  std::string s { "foo" };
  auto a = makeA(s, f); // wraps s.begin()
  std::clog << "main: " << *(s.begin()) << std::endl; …
Run Code Online (Sandbox Code Playgroud)

c++ clang std-function

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

std :: function是否支持自定义分配器?

这个open-std文档表明std :: function支持自定义分配器,但是我无法在互联网上找到任何关于如何提供自定义分配器的常用参考.

我的研究倾向于让我相信自定义分配器已经实现boost::functionstd::function尚未实现.

所以问题是std::function在C++ 11中使用自定义分配器吗?如果没有,它最有可能支持C++ 14中的一个?

c++ allocator c++11 std-function c++14

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

将重载函数(如std :: stoll)打包到std :: function中

我很难打包std::stoll成一个std::function.天真

std::function<std::int64_t(std::string const&)> obj = std::stoll;
Run Code Online (Sandbox Code Playgroud)

失败,因为std::stoll是两个函数的重载(cppreference没有提到[在询问的时候]),一个将std::string另一个std::wstring作为第一个参数.那么我怎么得到我想要的东西?

我知道我可以使用lambda来调用std::stoll,但我正在寻找表单的解决方案

auto parser = ???
std::function<std::int64_t(std::string const&)> obj{parser};
Run Code Online (Sandbox Code Playgroud)

c++ overloading c++11 std-function

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

在std :: map中使用std :: function

我想在std :: map中使用std :: function,请遵循以下代码:

#include <functional>
#include <map>

class MyClass {
    ...
    std::function<void()> Callback();
    std::map<std::string, Callback> my_map;
    ...
}
Run Code Online (Sandbox Code Playgroud)

std :: map收到一个Key和一个T,但不知道我的代码中有什么错误,他没有访问std :: map函数(insert,end,find ...)

使用typedef,他运行.但是为什么std :: function没有运行?

我最喜欢的地方:问题是什么?

之前:如何解决? - 代码示例请= D;

谢谢你的帮助

c++ dictionary c++11 std-function

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

一个向量/映射中的std :: function变量参数

如果不使用两个单独的持有者,今天如何在c ++中做到这一点?

typedef std::function<void(int a, int b)> f1; 
typedef std::function<void(int a)> f2; 

std::vector<f1> m;

void add(f1 f)
{
    m.push_back(f);
}

void add(f2 f)
{
    // add one more (unused) parameter to f2 so we can add f2 to f1 vector holder?
}
Run Code Online (Sandbox Code Playgroud)

我们可以以某种方式重载f1函数以包含不同的参数集吗?这可以通过现在的变量模板或类似的东西来解决吗?

c++ c++11 std-function c++14

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

具有std :: function参数的函数不接受lamba函数

我试图通过实现std::iterator我自己的双链表集合并尝试使用我自己的sort函数对它进行排序来更熟悉C++ 11标准.

我希望sort函数接受lamba作为一种排序的方式,通过使sort接受a std::function,但它不编译(我不知道如何实现move_iterator,因此返回集合的副本而不是修改传递的集合).

template <typename _Ty, typename _By>
LinkedList<_Ty> sort(const LinkedList<_Ty>& source, std::function<bool(_By, _By)> pred)
{
    LinkedList<_Ty> tmp;
    while (tmp.size() != source.size())
    {
        _Ty suitable;
        for (auto& i : source) {
            if (pred(suitable, i) == true) {
                suitable = i;
            }
        }
        tmp.push_back(suitable);
    }
    return tmp;
}
Run Code Online (Sandbox Code Playgroud)

我对函数的定义是错误的吗?如果我尝试调用该函数,我会收到编译错误.

LinkedList<std::string> strings{
    "one",
    "two",
    "long string",
    "the longest of them all"
};

auto sortedByLength = sort(strings, [](const std::string& a, const …
Run Code Online (Sandbox Code Playgroud)

lambda c++11 std-function visual-studio-2013

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

如何在std :: function中存储虚拟成员函数?

class foo
{
public:
    foo(void)
    {
        this->f = std::bind(&foo::doSomething, this);
    }

private:
    virtual void doSomething(void) { }

private:
    std::function<void(void)> f;
}

class bar : public foo
{
public:
    bar(void) { /* I have no idea what I have to */ }

private:
    virtual void doSomething(void) override { }
}
Run Code Online (Sandbox Code Playgroud)

我想将覆盖的'doSomething'函数分配给'foo :: f'.但我不知道如何分配重写'doSomething'功能.或者我只是编写一些代码来为每个类分配'doSomething'函数?

class foo
{
public:
    foo(void)
    {
        this->f = std::bind(&foo::doSomething, this);
    }

private:
    virtual void doSomething(void) { }

private:
    std::function<void(void)> f;
}

class bar : public foo
{
public: …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 std-function

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

可以是std :: function inlined或者我应该使用不同的方法吗?

我正在研究一个复杂的框架,它使用std::function<>许多函数作为参数.通过剖析我发现以下一个性能问题.

有人可以解释为什么Loop3a这么慢吗?我预计将使用内联,时间也是一样的.装配也一样.有没有办法改善表现或不同的方式?C++ 17是否以这种方式做出任何改变?

#include <iostream>
#include <functional>
#include <chrono>
#include <cmath>

static const unsigned N = 300;

struct Loop3a
{
    void impl()
    {
        sum = 0.0;
        for (unsigned i = 1; i <= N; ++i) {
            for (unsigned j = 1; j <= N; ++j) {
                for (unsigned k = 1; k <= N; ++k) {
                    sum +=  fn(i, j, k);
                }
            }
        }
    }

    std::function<double(double, double, double)> fn = [](double a, double b, double c) {
        const …
Run Code Online (Sandbox Code Playgroud)

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

2
推荐指数
2
解决办法
527
查看次数

返回lambda表达式的方法中的转换错误

这段代码没有问题:

#include "iostream"
#include  "functional"
std::function<double (double)> retFun(double* a) {
     return [a](double x) { return x+*a; };
 }

 int main(){

 double*e = new double(3);
 std::cout << retFun(e)(3) << std::endl;}
Run Code Online (Sandbox Code Playgroud)

但如果我retFun在一个对象中声明:

.H

class InternalVariables : public Page
{
private:
    std::function<double ()> retFun(double* a);
};
Run Code Online (Sandbox Code Playgroud)

的.cpp

std::function<double ()> InternalVariables::retFun(double *a)
{
    return [a](double b){ return b+ *a;};
}
Run Code Online (Sandbox Code Playgroud)

我得到以下错误

错误:无法将'InternalVariables :: retFun(double*):: __ lambda44 {a}'从'InternalVariables :: retFun(double*):: __ lambda44'转换为'std :: function'返回[a](双b) {return b +*a;};

c++ lambda c++11 std-function

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

在std :: array中使用时,无法从std :: function构造一个类

我想要一个std:arrayof std::function,但是我要确保该数组的所有元素都已初始化。为此,我构建了一个包装器类,该包装器类将a std::function作为构造参数。

但是,当我直接使用函数(应该在inside的函数)初始化包装器类的数组时,std::function它将无法编译。

这是问题,经过提炼:

#include <functional>
#include <array>

static void f() {}
using F = std::function<void(void)>;
enum { Count = 4 };

struct C
{
    //To get a compilation error when some
    //  elements of the array are not initialized.
    C() = delete;

    C(F) {}
};

//OK
static const C c {f};

//OK
static const std::array<F,Count> direct
{
    F{f},
    {f},
    f,
    f
};

static const std::array<C,Count> wrapper
{
    F{f},   //OK
    C{f},   //OK
    {f}, …
Run Code Online (Sandbox Code Playgroud)

c++ initialization c++11 std-function stdarray

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