我已经写了一些代码进行计数用算符和矢量的元素的数量ref
和bind
模板从boost::
或std::
(对于C++ 11)的命名空间.我正在使用a #define
来切换boost::
和std::
名称空间.我正在使用boost版本1.53,我的编译命令是g++ test.cpp -std=c++11
.我已经尝试过使用gcc版本4.7.2和4.6.3,但两者都有相同的错误.
我有3个问题:
std
和boost
版本bind
,ref
和function
?(我看到了这个问题,但答案没有提及ref
或function
)谢谢!
PS这个例子只是说明了我的问题,我知道size()
了std::vector
:-)
//#define USE_STD
#ifdef USE_STD
#include <functional>
using namespace std::placeholders;
namespace impl = std;
#else
#include <boost/version.hpp>
#include <boost/bind.hpp>
#include <boost/ref.hpp>
namespace impl = boost;
#endif
#include <iostream>
#include <algorithm>
#include <vector>
class Item { …
Run Code Online (Sandbox Code Playgroud) m_io_service.post(boost::ref(i));
Run Code Online (Sandbox Code Playgroud)
我在一段代码中有这个调用,底层类型i
肯定是可调用的(因为删除boost :: ref导致传递值,这很好),但是clang告诉我:
/opt/dev_64_swat/proto-rpc2/dependencies/boost/include/boost/asio/handler_invoke_hook.hpp:64:3: error: type 'boost::reference_wrapper<rubble::rpc::TcpFrontEndConnectionInvoker>' does not provide a call operator
Run Code Online (Sandbox Code Playgroud)
我如何通过引用传递,我的对象比异步调用更长,如果我可以通过引用传递它们,它们会更优雅(减少boost :: shared_ptr <..>作为成员).
- 编辑 -
我已经浏览了asio的示例目录,并boost::ref
没有为完成处理程序演示.所以我想我在这里运气不好.有没有理由为什么处理程序没有版本接受ref?
- 编辑2:我的样子(除非你对实施有所怀疑,否则不要去看这个). -
namespace rubble { namespace rpc {
struct InProcessInvoker : public InvokerBase
{
struct notification_object_
{
typedef notification_object_ * ptr;
notification_object_()
{
reset();
}
void reset()
{
ready = false;
}
bool ready;
boost::mutex mutex;
boost::condition_variable cond;
};
InProcessInvoker(BackEnd & b_in)
: b(b_in),
notification_object(new notification_object_())
{
b.connect(m_client_data);
}
~InProcessInvoker()
{
if( m_client_data.unique() …
Run Code Online (Sandbox Code Playgroud) c++ functional-programming pass-by-reference boost-asio boost-ref
我对boost :: ref的使用感到困惑.我不明白为什么任何人想要做以下事情 -
void f(int x)
{
cout << x<<endl;
x++;
}
int main(int argc, char *argv[])
{
int aaa=2;
f(boost::ref(aaa));
cout << aaa<<endl;
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
将ref传递给函数有什么用?我总是可以通过价值来代替.也不是说裁判实际上是通过了.在上面,主要的aaa值仅为2.
boost ref究竟在哪里有用?
在这种情况下是否可以使用boost :: ref.我想传递iterator引用std :: sort函数.通常这个排序适用于迭代器副本 - boost :: ref是否也适用于引用?(对std :: sort没有任何更改)
Ref库是一个小型库,可用于传递通常需要复制其参数的函数模板(算法).
来自http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/example/chat/chat_server.cpp
在电话交付 -
void deliver(const chat_message& msg)
{
recent_msgs_.push_back(msg);
while (recent_msgs_.size() > max_recent_msgs)
recent_msgs_.pop_front();
std::for_each(participants_.begin(), participants_.end(),
boost::bind(&chat_participant::deliver, _1, boost::ref(msg)));
}
Run Code Online (Sandbox Code Playgroud)
如果
void deliver(const chat_message& msg)
Run Code Online (Sandbox Code Playgroud)
在另一个类中通过引用获取消息然后为什么使用boost :: ref?