对我来说另一个令人不安的错误信息,如果我正确理解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&
函数,它作为参数 - 返回一个布尔值.
要创建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 ++"但没有发现任何线索.
我需要一种方法来分解函数std::bind()
模板中返回的函数对象及其参数。
下面的代码片段显示了我想要做的事情:
\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 … 我发现使用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创建的函数调用时,为什么变量不会递增?
这是一个玩具的例子
#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
对象对应于同一对象的相同方法?
我试图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) 我想存储一个对象的回调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) 我正在使用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] …
std::function
我想知道与指向成员函数的指针一起使用时如何声明返回类型的对象:
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 … 我试图将两个不同的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)