我正在使用GCC 4.6.3并尝试使用以下代码生成随机数:
#include <random>
#include <functional>
int main()
{
std::mt19937 rng_engine;
printf("With bind\n");
for(int i = 0; i < 5; ++i) {
std::uniform_real_distribution<double> dist(0.0, 1.0);
auto rng = std::bind(dist, rng_engine);
printf("%g\n", rng());
}
printf("Without bind\n");
for(int i = 0; i < 5; ++i) {
std::uniform_real_distribution<double> dist(0.0, 1.0);
printf("%g\n", dist(rng_engine));
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我期望两种方法都能生成5个随机数的序列.相反,这是我实际得到的:
With bind
0.135477
0.135477
0.135477
0.135477
0.135477
Without bind
0.135477
0.835009
0.968868
0.221034
0.308167
Run Code Online (Sandbox Code Playgroud)
这是GCC的错误吗?或者是与std :: bind有关的一些微妙问题?如果是这样,你能对结果有所了解吗?
谢谢.
(注意: 正如标签中已经清楚的那样,这是严格的 C++ 03. 是的,我知道,lambda会让所有这些痛苦消失(带来新的种类,我打赌),但这是一个嵌入式系统,有一个90年代的操作系统版本,我被告知我应该很高兴我有一个C++ 03编译器(GCC4.1.x,BTW)或C++编译器.所以请不要发布C++ 11解决方案,无需擦它,真的.
另外,std::bind(),std::function()等等,当然,实际std::tr1的,但我编辑了的 tr1前缀,因为我认为这增加了大多只有噪声的代码.)
我有一些类似服务器的东西,我需要注册函数,我需要调整它们来调用一些对象类似但略有不同的函数.这些函数具有不同的参数列表.服务器"知道"当我尝试注册一个函数时,它只接受一个具有正确签名的函数(根据std::function需要正确),这取决于作为模板参数传入的一些魔术标记.
这是代码的草图:
// this I just use
class server {
public:
template<unsigned int MagicTag>
bool register_call(typename some_traits_type<MagicTag>::func_type);
};
// this needs to be called from the server
class X {
public:
bool foo();
bool bar(std::string&);
bool baz(int);
};
// this is the glue
class Y {
public:
Y(X& x) : x_(x) {
register_call<MAGIC_FOO>(&Y::foo );
register_call<MAGIC_BAZ>(&Y::bar, _1);
register_call<MAGIC_FBZ>(&Y::baz, _1); …Run Code Online (Sandbox Code Playgroud) 我尝试使用VC11和g ++ 4.7.2编译以下示例:
#include <functional>
class X {
public:
template <typename T>
explicit X(T t)
{
std::bind(&X::invoke<T>, this, t)();
}
private:
template <typename T>
void invoke(T t)
{
t();
}
};
class Y {
public:
void foo() {
//...
}
};
int main() {
Y y;
X x(std::bind(&Y::foo, &y));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但它完成了错误.我不确定粘贴整个编译器输出是否合理,但一般情况下
vc11说:
错误C2664:'void std :: _ Pmf_wrap :: operator()(_ Farg0&,_ V0_t)const':无法将参数3从'void'转换为'std :: _ Bind,Y*,std :: _ Nil,std :: _ Nil ,std :: _ Nil,std :: _ Nil,std :: …
我正在尝试将非静态类成员绑定到标准WNDPROC函数.我知道我可以通过使类成员静态来做到这一点.但是,作为一名C++ 11 STL学习者,我对使用<functional>标题下的工具非常感兴趣.
我的代码如下.
class MainWindow
{
public:
void Create()
{
WNDCLASSEXW WindowClass;
WindowClass.cbSize = sizeof(WNDCLASSEX);
WindowClass.style = m_ClassStyles;
WindowClass.lpfnWndProc = std::function<LRESULT(HWND, UINT, WPARAM, LPARAM)>
( std::bind(&MainWindow::WindowProc,
*this,
std::placeholders::_1,
std::placeholders::_2,
std::placeholders::_3,
std::placeholders::_4));
WindowClass.cbClsExtra = 0;
WindowClass.cbWndExtra = 0;
WindowClass.hInstance = m_hInstance;
WindowClass.hIcon = LoadIconW(m_hInstance, MAKEINTRESOURCEW(IDI_WINDOW));
WindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WindowClass.hbrBackground = (HBRUSH) COLOR_WINDOW;
WindowClass.lpszMenuName = MAKEINTRESOURCEW(IDR_MENU);
WindowClass.lpszClassName = m_ClassName.c_str();
WindowClass.hIconSm = LoadIconW(m_hInstance, MAKEINTRESOURCEW(IDI_WINDOW_SMALL));
RegisterClassExW(&WindowClass);
m_hWnd = CreateWindowEx(/*_In_ DWORD*/ ExtendedStyles,
/*_In_opt_ LPCTSTR*/ m_ClassName.c_str(),
/*_In_opt_ LPCTSTR*/ m_WindowTitle.c_str(),
/*_In_ …Run Code Online (Sandbox Code Playgroud) 几天来我一直在试着弄清楚如何让一个类有一个很好的干净的公共接口来执行回调机制的注册.回调可以是C++ 11个lambda表达式,std::function<void(Type1,Type2)>,std::function<void(Type2)>,std::function<void()>,或的结果std::bind().
这个接口的关键是该类的用户只需要知道一个公共接口,它接受用户可能抛出的几乎任何functor/callback机制.
struct Type1;
struct Type2; // May be the same type as Type1
class MyRegistrationClass
{
public:
/**
* Clean and easy to understand public interface:
* Handle registration of any functor matching _any_ of the following
* std::function<void(Type1,Type2)>
* std::function<void(Type2)> <-- move argument 2 into arg 1
* std::function<void()>
* or any result of std::bind() requiring two or fewer arguments that
* can convert to the above std::function< …Run Code Online (Sandbox Code Playgroud) 我无法找到如何使用参数将参数绑定到重载函数std::bind.不知何故std::bind不能推断出重载类型(对于它的模板参数).如果我不重载该功能一切正常.代码如下:
#include <iostream>
#include <functional>
#include <cmath>
using namespace std;
using namespace std::placeholders;
double f(double x)
{
return x;
}
// std::bind works if this overloaded is commented out
float f(float x)
{
return x;
}
// want to bind to `f(2)`, for the double(double) version
int main()
{
// none of the lines below compile:
// auto f_binder = std::bind(f, static_cast<double>(2));
// auto f_binder = bind((std::function<double(double)>)f, \
// static_cast<double>(2));
// auto f_binder = bind<std::function<double(double)>>(f, \ …Run Code Online (Sandbox Code Playgroud) 我有api功能f_api(std::function<void(int)> func),现在我有我的流程类
class Func {
public:
void operator()(int i) {
// do some work
}
int operator()(int i, int j) {
// do some other work
}
};
Run Code Online (Sandbox Code Playgroud)
我想用Func ff(int i)传递给f_api来完成工作; 所以我使用std :: bind
Func f;
std::function<void(int)> func = std::bind(&Func::operator(), &f, std::placeholders::_1);
f_api(func);
Run Code Online (Sandbox Code Playgroud)
但问题是,如何指出Func::operator()()我想要绑定哪个?我可以通过它的名称给出一个成员函数,但是当这个成员函数有多个不同的签名重新加载函数时,如何处理它呢?请问std :: bind能找到最适合调用的函数吗?C++太复杂了......
最小可验证的案例:
#include <iostream>
#include <functional>
using namespace std;
class Func {
public:
void operator()(int i) {
// do some work
cout << "i is: " << i << …Run Code Online (Sandbox Code Playgroud) 我正在探索对g ++ - 4.7(Ubuntu/Linaro 4.7.3-2ubuntu~12.04,具体而言)对C++ 11的支持,我似乎发现了差异.
特别是,如果我在Boost ASIO async客户端示例中注释#include <boost/bind.hpp>并系统地替换boost::bindwith的出现std::bind(取自http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/example/http/client /async_client.cpp),程序不再编译.
对此有何解释?
在经历了一个问题后std::bind,我想知道是否有可能保留一个vector由std::bind我创建的函数,因此我可以避免使用std::function它和它的重量级包装.
#include <iostream>
#include <functional>
#include <typeinfo>
#include <vector>
int add(int a, int b) {return a + b;}
int main() {
//I believe this here is just a special type of bound function.
auto add2 = std::bind(add, std::placeholders::_1, 2);
auto add3 = std::bind(add, std::placeholders::_1, 3);
//Yup.
std::cout << typeid(add2).name() << std::endl;
//Here's the type of the second function
std::cout << typeid(add3).name() << std::endl;
//Is there a nicer way to do …Run Code Online (Sandbox Code Playgroud) 我想std::bind从一个私有基类的成员函数using,在派生类中使用-declaration "public" .调用函数直接工作,但似乎绑定或使用成员函数指针不编译:
#include <functional>
struct Base {
void foo() { }
};
struct Derived : private Base {
using Base::foo;
};
int main(int, char **)
{
Derived d;
// call member function directly:
// compiles fine
d.foo();
// call function object bound to member function:
// no matching function for call to object of type '__bind<void (Base::*)(), Derived &>'
std::bind(&Derived::foo, d)();
// call via pointer to member function:
// cannot cast 'Derived' to its private …Run Code Online (Sandbox Code Playgroud)