标签: stdbind

std :: bind - 在visual studio中编译意外(未声明的标识符)错误

对我来说另一个令人不安的错误信息,如果我正确理解std :: bind我可以参数_1来定义一个非给定的参数吗?对?考虑以下几行:

std::function<bool(value_type, const std::string &)> 
                             func(std::bind(&Pred, _1, "name"));
Run Code Online (Sandbox Code Playgroud)

这应该有用,对吗?这将用于std :: find_if()函数,因此第一个参数应该是值类型,第二个参数应该是字符串.

然而,visual studio 2010通过以下错误消息抱怨此问题:

错误C2065:'_ 1':未声明的标识符

这很奇怪,我怎么能在视觉工作室说"嘿第一个参数没有约束".Pred是一个简单的value_type, const std::string&函数,它作为参数 - 返回一个布尔值.

c++ visual-studio-2010 stdbind c++11

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

在std :: bind中省略std :: placeholders

要创建std :: function,我就是这样做的: -

std::function<void(int,int,int)> f=
    std::bind(&B::fb,this,
        std::placeholders::_1,
        std::placeholders::_2,
        std::placeholders::_3
    );  

void B::fb(int x,int k,int j){} //example
Run Code Online (Sandbox Code Playgroud)

很明显,B::fb接收三个参数.
为了提高可读性和可维护性,我希望我可以这样称呼: -

std::function<void(int,int,int)> f=std::bind(&B::fb,this);  //omit _1 _2 _3
Run Code Online (Sandbox Code Playgroud)

问题
C++中是否有任何功能可以省略占位符?
它应该自动在命令中调用_1,_2,....

我用谷歌搜索"省略占位符c ++"但没有发现任何线索.

c++ stdbind c++11 std-function

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

有没有办法访问存储在 C++ 中 std::bind() 返回的函数对象中的参数?

我需要一种方法来分解函数std::bind()模板中返回的函数对象及其参数。

\n

下面的代码片段显示了我想要做的事情:

\n
#include <iostream>\n#include <functional>\n\nvoid foo(int x, double y, char z)\n{\n    std::cout << "x = " << x << ", y = " << y << ", z = " << z << \'\\n\';\n}\n\nint main()\n{\n    auto f = std::bind(foo, 42, 3.14, \'a\');\n    std::cout << "The first argument is: " << std::get<0>(f) << \'\\n\';\n    std::cout << "The second argument is: " << std::get<1>(f) << \'\\n\';\n    std::cout << "The third argument is: " << std::get<2>(f) << \'\\n\';\n}\n
Run Code Online (Sandbox Code Playgroud)\n …

c++ std stdbind c++11

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

为什么绑定无法通过引用传递?

我发现使用std :: bind时,引用传递往往不起作用.这是一个例子.

int test;

void inc(int &i)
{
    i++;
}

int main() {
    test = 0;
    auto i = bind(inc, test);
    i();
    cout<<test<<endl; // Outputs 0, should be 1
    inc(test);
    cout<<test<<endl; // Outputs 1
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当通过使用std bind创建的函数调用时,为什么变量不会递增?

c++ stdbind

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

比较用std :: bind创建的std :: function

这是一个玩具的例子

#include <iostream>
#include <functional>

struct Obj
{
  int x;

  int foo()
  {
    return 42;
  }
};

int main()
{
  Obj a, b;
  std::function<int()> f1 = std::bind(&Obj::foo, &a);
  std::function<int()> f2 = std::bind(&Obj::foo, &b);
  std::function<int()> f3 = std::bind(&Obj::foo, &a);
  std::function<int()> f4 = std::bind(&Obj::foo, &b);
}
Run Code Online (Sandbox Code Playgroud)

我如何验证,f1 == f3并且f2 == f4,==比较器在这里,这意味着两个std::function对象对应于同一对象的相同方法?

c++ stdbind c++11 std-function

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

std ::绑定到lambda:编译错误

我试图std::bind去一个lambda时遇到了错误.以下代码示例编译正常:

#include <functional>
#include <iostream>

int main()
{
    const auto lambda1 = [](int a, int b){ return a + b; };

    std::cout << (std::bind( lambda1, std::placeholders::_1, 2))(2)
              << std::endl;

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

但以下不是:

#include <functional>
#include <iostream>

int main()
{
    const auto lambda2 { [](int a, int b){ return a + b; } }; // Note the "{ }-initialization"

    std::cout << (std::bind( lambda2, std::placeholders::_1, 2))(2)
              << std::endl;

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

这是我使用Visual Studio 2013得到的错误输出,更新4:

1>------ Build started: Project: Project1, …
Run Code Online (Sandbox Code Playgroud)

c++ lambda initializer-list stdbind c++11

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

如何将 std::bind 结果存储到 std::function

我想存储一个对象的回调std::function<int(int,int>>。当我使用 lambda 时,它工作得很好。但是当我使用std::bind成员函数时,它编译错误。

有示例错误代码。

#include <functional>
#include <iostream>

using namespace std;

class A
{
public:
    void foo()
    {
        std::function<int(int,int)> callback = std::bind(&A::calc, this);
        // std::function<int(int,int)> callback = [this](int a, int b) { return this->calc(a, b); }; // lambda works fine
        int r = callback(3, 4);
        cout << r << endl;

    }

    int calc(int b, int c) { return b + c; }
};

int main()
{
    A a;
    a.foo();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误信息:

 In member function …
Run Code Online (Sandbox Code Playgroud)

c++ stdbind

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

std :: function与std :: bind完美结合-但是为什么呢?

我正在使用a std::uniform_int_distribution生成素数(p)。我将分发对象放在一个匿名的命名空间中-对于成年人来说似乎是C ++的“静态链接” ...

namespace
{
    // a more pedantic range: [2, 18446744073709551557]
    std::uniform_int_distribution<uint64_t> p_dist {2};

    std::mt19937 rng; // (sufficient IV states for uniqueness)
}
Run Code Online (Sandbox Code Playgroud)

请注意,在可移植代码允许的范围内,我尽可能彻底地播种了Mersenne Twister。不过,这对于这个问题并不是很重要。这只是为了确保读者可以正确使用随机工具:

std::seed_seq::result_type data[rng.state_size];
std::random_device rdev;

std::generate_n(data, rng.state_size, std::ref(rdev));
std::seed_seq rng_seed (data, data + rng.state_size);
rng.seed(rng_seed);
Run Code Online (Sandbox Code Playgroud)

这非常方便,因为我使用确定性u64_prime(p)函数,(7) bases可以确定是否(p)为质数:

uint64_t p;
while (!u64_prime(p = p_dist(rng)))
    ;
Run Code Online (Sandbox Code Playgroud)

现在,我创建一个std::function对象:

std::function<uint64_t()> zp_rng = std::bind(
    decltype(p_dist){0, p - 1}, std::ref(rng));
Run Code Online (Sandbox Code Playgroud)

此函数:zp_rng()可以被调用以返回中的随机数Z(p)。也就是说,将分发对象用于:[0, p - 1] …

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

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

如何存储从 std::bind 返回的可调用对象作为指向成员函数的指针?

std::function我想知道与指向成员函数的指针一起使用时如何声明返回类型的对象:

\n
int main(){\n\n    void foo(std::string, int);\n    string s = "hi";\n    auto fn = std::bind(foo, s, std::placeholders::_1);\n    fn(5);// ok\n\n    /*auto fn2 = */std::bind(&std::string::empty, std::placeholders::_1); // error if I uncomment /*auto fn2 = */\n\n    std::vector<std::string> vs{"The", "Quick", "Brown", "Fox", "Jumped", "", "Over", "The", "", "Lazy", "Dog"};\n    auto it = std::find_if(vs.cbegin(), vs.cend(),\n    std::bind(&std::string::empty, std::placeholders::_1)); // OK\n    std::cout << "Empty string found at index: " << (it != vs.cend() ? std::distance(vs.cbegin(), it) : -1) << '\\n'; // 5\n\n    std::cout << "\\ndone!\\n";\n}\n
Run Code Online (Sandbox Code Playgroud)\n …

c++ pointer-to-member stdbind c++11

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

C++ - 如何将 default_random_engine 正确绑定到两个不同的 uniform_int_distributions

我试图将两个不同的std::uniform_int_distribution绑定对象(使用std::bind)与相同的对象std::default_random_engine用作参数(如此处所述http://www.cplusplus.com/reference/random/),但将它们绑定在一起会导致与使用它们不同的行为未绑定:

#include <iostream>
#include <functional>
#include <random>

using namespace std;

int main()
{
    default_random_engine generator;

    int dist1Max = 10, dist2Max = 10;

    uniform_int_distribution<int> dist1(1, dist1Max);
    uniform_int_distribution<int> dist2(1, dist2Max);

    function<int()> boundDist1 = std::bind(dist1, generator);
    function<int()> boundDist2 = std::bind(dist2, generator);

    for (int i=0; i<10; ++i)
    {
        cout << boundDist1() << " " << boundDist2() << endl;
    }
    cout << endl;

    for (int i=0; i<10; ++i)
    {
        cout << dist1(generator) << " " << dist2(generator) …
Run Code Online (Sandbox Code Playgroud)

c++ stdbind

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