小编Sam*_*ler的帖子

序列化variables_map

如何序列化/反序列化boost :: program_options :: variables_map?我找不到已经实现的序列化函数,我不知道我可以使用variables_map中的哪些函数来提取和重新组合映射.

c++ boost boost-program-options boost-serialization

10
推荐指数
1
解决办法
1398
查看次数

提升等效于std :: async()

如果不使用boost::threadboost::bind直接,有没有实现下面的代码等效的方法吗?

std::string func()
{
    std::string str("Hello from async task!");
    return str;
}

int main()
{
    auto ftr = std::async(&func);
    std::cout << "Hello from main!";
    std::string str = ftr.get();
    std::cout << str << std::endl;      
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

具体而言,这一部分:auto ftr = std::async(&func);

c++ boost asynchronous c++11

10
推荐指数
1
解决办法
8615
查看次数

使用多个io_service对象

我的应用程序在其中监听和处理来自互联网套接字和unix域套接字的消息.现在我需要将SSL添加到互联网套接字,我正在io_service为应用程序中的所有套接字使用单个对象.现在看来我需要io_service为网络套接字和unix域套接字添加单独的对象.我没有在我的应用程序的任何线程,我用async_sendasync_recieveasync_accept处理数据和连接.请指出使用io_service具有异步处理程序的多个对象的任何示例.

boost-asio

10
推荐指数
1
解决办法
1万
查看次数

Boost程序选项不适用于GLIBCXX_DEBUG

我有以下示例代码:

#include <iostream>
#include <boost/program_options.hpp>

int main ( int ac, char *av[] ) {
    // Declare the supported options.
    boost::program_options::options_description desc("Allowed options");
    desc.add_options()("help", "produce help message");

    boost::program_options::variables_map vm;
    boost::program_options::store(boost::program_options::parse_command_line(ac, av, desc), vm);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它使用例如编译好g++ test.cpp -lboost_program_options.但是,如果我尝试使用该调用激活GCC边界检查g++ test.cpp -lboost_program_options -D_GLIBCXX_DEBUG,则会抛出以下链接器错误:

/tmp/ccZLdZ1g.o: In function `boost::program_options::basic_command_line_parser<char>::basic_command_line_parser(int, char const* const*)':
test.cpp:(.text._ZN5boost15program_options25basic_command_line_parserIcEC2EiPKPKc[_ZN5boost15program_options25basic_command_line_parserIcEC5EiPKPKc]+0x97): undefined reference to `boost::program_options::detail::cmdline::cmdline(std::__debug::vector<std::string, std::allocator<std::string> > const&)'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

据我所知,链接器找不到该函数boost::program_options::detail::cmdline::cmdline(std::__debug::vector<std::string, std::allocator<std::string> > const&),因为它的参数被调试向量代替而不是正常std::vector.但为什么会这样呢?有没有人知道一个解决方法,使Boost程序选项适用GLIBCXX_DEBUG

我使用以下系统:

  • Debian Wheezy …

c++ linker boost g++ boost-program-options

10
推荐指数
1
解决办法
2133
查看次数

当使用Boost ASIO时,有效负载分配两个TCP数据包,当它适合MTU时

我有一个boost :: asio :: ip :: tcp :: iostream的问题.我试图发送大约20个原始字节.问题是这个20字节的有效载荷被分成两个TCP数据包,其中包含1个字节,然后是19个字节.简单的问题,为什么会发生我不知道.我正在为传统的二进制协议写这个,非常需要有效载荷适合单个TCP数据包(呻吟).

从我的程序中粘贴整个源代码将是漫长而过于复杂的,我在这里发布了两个函数中的功能问题(经过测试,它确实重现了这个问题);

#include <iostream>

// BEGIN cygwin nastyness
// The following macros and conditions are to address a Boost compile
// issue on cygwin. https://svn.boost.org/trac/boost/ticket/4816
//
/// 1st issue
#include <boost/asio/detail/pipe_select_interrupter.hpp>

/// 2nd issue
#ifdef __CYGWIN__
#include <termios.h>
#ifdef cfgetospeed
#define __cfgetospeed__impl(tp) cfgetospeed(tp)
#undef cfgetospeed
inline speed_t cfgetospeed(const struct termios *tp)
{
    return __cfgetospeed__impl(tp);
}
#undef __cfgetospeed__impl
#endif /// cfgetospeed is a macro

/// 3rd issue
#undef __CYGWIN__
#include <boost/asio/detail/buffer_sequence_adapter.hpp>
#define __CYGWIN__ …
Run Code Online (Sandbox Code Playgroud)

c++ boost tcp boost-asio

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

boost :: asio socket async_*strand

如何通过strand在socket上执行async_*操作?我看过Timer.5(Boost/Asio示例),但它们只展示了如何调用用户的处理程序.当我async_write在多线程应用程序数据中的套接字可能被写入损坏.并strand保证这些处理程序不会同时执行.

sockets boost asynchronous boost-asio

9
推荐指数
1
解决办法
2973
查看次数

使用boost :: asio中的write()发送原始数据

我最近决定使用boost::asio我的套接字,但现在我遇到了一个问题:文档似乎缺乏.

我想要做的是编写一个函数,它将发送一个包含以下结构的消息:

  • uint16_t操作码所有字节的无符号整数()的2个字节
  • 之后的所有字节(灵活数量)是任何类型的数据(强制转换为void*).该数据将基于操作码进行操作

例如,如果操作码是1,可能定义为OPCODE_LOGIN,那么操作码后面的字节可能包含一个包含登录信息等的字符串.

bool sendMessage(tcp::socket* sock, uint16_t opcode, void* data)
{
    void* fullData = malloc(sizeof(uint16_t) + sizeof(data));
    memcpy(fullData, (void*)opcode, sizeof(opcode));
    memcpy(&fullData + sizeof(uint16_t), data, sizeof(data));
    boost::asio::write(sock, boost::asio::buffer(fullData, sizeof(fullData)));
    // by the way, at this point, is it safe to delete fullData to prevent memory leaks?
    return true;
Run Code Online (Sandbox Code Playgroud)

}

但是,这不会编译.关于写入调用,我得到一个神秘的编译错误:

1>------ Build started: Project: client, Configuration: Debug Win32 ------
1>  main.cpp
1>c:\boost\boost_1_47\boost\asio\impl\write.hpp(46): error C2228: left of '.write_some' must have class/struct/union
1>          type is 'boost::asio::basic_stream_socket<Protocol> …
Run Code Online (Sandbox Code Playgroud)

c++ boost-asio

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

使用Boost Asio设置发布队列大小的限制?

boost::asio::io_service用作基本线程池.一些线程被添加到io_service,主线程开始发布处理程序,工作线程开始运行处理程序,一切都完成.到现在为止还挺好; 我获得了比单线程代码更好的加速.

但是,主线程有数以百万计的东西要发布.它只是继续发布它们,比工作线程可以处理它们快得多.我没有达到内存限制,但是将这么多内容排入队列仍然有些愚蠢.我想要做的是处理程序队列具有固定大小,并且如果队列已满,则使用post()块.

我在Boost ASIO文档中没有看到任何选项.这可能吗?

c++ boost-asio threadpool

9
推荐指数
1
解决办法
3175
查看次数

编译器或升级库的错误?

在Windows 7下的vs2008 Release(Win32)模式下编译后,这个程序(它已经从较大的程序缩小)总是崩溃.我不熟悉汇编代码,不知道它是编译器或boost :: ends_with的错误或者boost :: asio :: buffers_iterator.它可以在Ubuntu中使用g ++编译和执行,没有任何问题.

人们说它不太可能成为编译器的错误,但是当在调试模式(或禁用优化)中编译时,问题确实消失了.

我已经坚持这个问题了好几个小时.任何帮助表示赞赏.提前致谢.

#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/algorithm/string.hpp>

typedef boost::asio::buffers_iterator<boost::asio::const_buffers_1> iterator_t;
typedef boost::iterator_range<iterator_t> range_t;
static const std::string LINE_END_MARK = "\r\n";

int main(int argc, char* argv[])
{
    boost::asio::streambuf _buf;
    std::ostream os(&_buf);
    os<<"END\r\n";

    iterator_t cursor = boost::asio::buffers_begin(_buf.data());
    iterator_t end = boost::asio::buffers_end(_buf.data());

    std::ostream_iterator<char> it(std::cout," ");
    std::copy(LINE_END_MARK.begin(), LINE_END_MARK.end(), it);

    range_t r(cursor, end);
    if(!boost::ends_with(r, LINE_END_MARK))
        return 0;
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-asio visual-c++

9
推荐指数
1
解决办法
593
查看次数

祖父母在孩子身上重载了功能

我需要理解为什么C++不允许在Child中访问Grandparent重载函数,如果在Parent中声明了任何重载函数.请考虑以下示例:

class grandparent{
public:
    void foo();
    void foo(int);
    void test();
};

class parent : public grandparent{
public:
    void foo();
};

class child : public parent{
public:
    child(){
        //foo(1); //not accessible
        test();   //accessible
    }
};
Run Code Online (Sandbox Code Playgroud)

这里,两个函数foo()和foo(int)是Grandparent中的重载函数.但是foo(int)是不可访问的,因为foo()在Parent中声明(如果声明它是public或private或protected,则无关紧要).但是,test()是可访问的,根据OOP是正确的.

我需要知道这种行为的原因.

c++ overriding overloading

9
推荐指数
1
解决办法
1477
查看次数