小编Igo*_* R.的帖子

通过boost消息队列发送复杂的数据结构

我有以下数据结构:

typedef struct
{
    short id;
    string name;
    short age;
} person_struct;
Run Code Online (Sandbox Code Playgroud)

使用boost消息队列,我试图在另一个进程中将此数据结构发送到消息队列接收器.然而,收到后,我在访问上述结构中的'name'变量时出现了分段错误.

以下是我的发件人功能:

person_struct MyRec;
MyRec.id = 1;
MyRec.name = "ABC123";
MyRec.age = 20;   
message_queue mqSender(create_only, "MSG_Q", 100, sizeof(person_struct));
mqSender.send(&MyRec, sizeof(person_struct), MQ_PRIORITY);
Run Code Online (Sandbox Code Playgroud)

以下是我的接收功能:

message_queue myReceiver(open_only, "MSG_Q");
person_struct *recvMsg = new person_struct();
size_t msg_size;
unsigned int priority;
myReceiver.receive(recvMsg, sizeof(person_struct), msg_size, priority);
cout << "ID: " << (*recvMsg).id << endl;
cout << "Name: " << (*recvMsg).name << endl;
cout << "Age: " << (*recvMsg).age << endl;
Run Code Online (Sandbox Code Playgroud)

(*recvMsg).id的cout没问题,但cout为(*recvMsg).name发生了分段错误.阅读我需要为结构进行序列化的地方,但无法弄清楚如何去做.谁有人建议?

boost message-queue boost-interprocess

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

如何用英语获取boost :: system :: error_code :: message?

在具有本地化UI的Win7上,error_code::message()返回非英语消息.据我所知(在Boost 1.54中,for system_error_category),上面的函数归结为以下WinAPI调用:

DWORD retval = ::FormatMessageA( 
    FORMAT_MESSAGE_ALLOCATE_BUFFER | 
    FORMAT_MESSAGE_FROM_SYSTEM | 
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    ev,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    (LPSTR) &lpMsgBuf,
    0,
    NULL 
);
Run Code Online (Sandbox Code Playgroud)

如何让上述内容FormatMessage返回英文信息?我尝试设置语言环境,包括std函数和SetThreadLocale- 它没有帮助.

更新:只是澄清:基本上,我的问题是如何以编程方式"覆盖" 用户默认语言以及为什么设置区域设置不够.

winapi boost locale boost-system

7
推荐指数
1
解决办法
874
查看次数

使用void_t和受保护的嵌套类进行基于SFINAE的检测

我最近在clang和gcc之间遇到了关于void_t属性检测和受保护/私有类信息的一些不同行为.考虑一个类型特征,定义如下:

#include <type_traits>

template<typename T, typename = void>
constexpr const bool has_nested_type_v = false;

template<typename T>
constexpr const bool has_nested_type_v
<T, std::void_t<typename T::type>> = true;
Run Code Online (Sandbox Code Playgroud)

给定具有受保护或私有嵌套type类和简单程序的示例类型

#include "has_nested_type.hpp"
#include <iostream>

struct Protected {
protected:
  struct type{};
};

struct Private {
private:
  struct type{};
};

int main() {
  std::cout << "Protected: " 
            << (has_nested_type_v<Protected> ? "detected" : "not detected")
            << std::endl;
  std::cout << "Private: " 
            << (has_nested_type_v<Private> ? "detected" : "not detected")
            << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
  • clang成功编译失败的检测(如预期的那样).该程序,编译和输出上再现wandbox 这里 …

c++ gcc detection c++17 void-t

7
推荐指数
1
解决办法
211
查看次数

我可以为局部变量创建boost :: shared_ptr吗?

我有一些方法可以引用给定的对象,有些方法正在采用boost::shared_ptr.到目前为止,在我的测试方法中,我创建了一个shared_ptr指向其中一个对象并传递*ptr给期望引用的方法.是否有可能以相反的方式进行,例如在堆栈上创建一个本地对象,然后以安全的方式创建一个指向它的共享指针,以便&obj用传统指针直接替代运算符?

c++ boost shared-ptr

6
推荐指数
3
解决办法
3197
查看次数

重载决策和显式模板参数

以下代码打印"func 2".

为什么编译器会在存在显式(未推断)模板参数的情况下将第二个模板视为更好的匹配?为什么没有歧义?

我很感激C++标准的引用.

#include <iostream>

template<class T>
struct identity
{
    typedef T type;
};

template<class T>
void func(T)
{
    std::cout << "func 1\n";
}

template<class T>
void func(typename identity<T>::type)
{
    std::cout << "func 2\n";
}

int main()
{
    func<int>(1);    
}
Run Code Online (Sandbox Code Playgroud)

c++ template-specialization overload-resolution

6
推荐指数
1
解决办法
99
查看次数

VLC:“avcodec 错误:视频延迟超过 5 秒”

我正在将 mpeg4 视频混合到 avi 容器。视频流约为 4fps,因此我设置AVCodecContext time_base为 1/4 ( formatContext->streams[0]->codec->time_base...) ,然后重新调整每个帧时间戳,因为原始时间戳以毫秒为单位。我将结果值分配给数据包ptsdts

当我用 VLC 播放此 avi 时,视频不流畅,并且反复收到以下消息:

avcodec 错误:视频延迟超过 5 秒 -> 丢帧(计算机太慢?)

上面的pts/dts计算有问题吗?

下表演示了时间戳如何重新缩放(原始->重新缩放)

stream: 0 1329471005111->1
stream: 0 1329471005348->2
stream: 0 1329471005588->3
stream: 0 1329471005828->4
stream: 0 1329471006068->5
stream: 0 1329471006308->6
stream: 0 1329471006551->7
stream: 0 1329471006788->8
stream: 0 1329471007028->9
stream: 0 1329471007268->10
stream: 0 1329471007508->11
stream: 0 1329471007748->12
stream: 0 1329471007988->13
stream: 0 1329471008228->14
stream: 0 1329471008468->15
Run Code Online (Sandbox Code Playgroud)

ffmpeg pts vlc libavformat

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

为什么这个ASIO示例使用成员变量来传递状态而不是使用bind?

ASIO HTTP Server 3示例中,有如下代码:

void server::start_accept()
{
  new_connection_.reset(new connection(io_service_, request_handler_));
  acceptor_.async_accept(new_connection_->socket(),
      boost::bind(&server::handle_accept, this,
        boost::asio::placeholders::error));
}

void server::handle_accept(const boost::system::error_code& e)
{
  if (!e)
  {
    new_connection_->start();
  }

  start_accept();
}
Run Code Online (Sandbox Code Playgroud)

从本质上讲,new_connection_是的构件server类和用于传递从一个连接start_accepthandle_accept.现在,我很好奇为什么new_connection_实现为成员变量.

使用bind而不是使用成员变量来传递连接是否也有效?像这样:

void server::start_accept()
{
  std::shared_ptr<connection> new_connection(new connection(io_service_, request_handler_));
  acceptor_.async_accept(new_connection_->socket(),
      boost::bind(&server::handle_accept, this,
        boost::asio::placeholders::error),
        new_connection);
}

void server::handle_accept(boost::system::error_code const& error, std::shared_ptr<connection> new_connection)
{
  if (!error) {
    new_connection->start();
  }
  start_accept();
}
Run Code Online (Sandbox Code Playgroud)

如果是这样,为什么该示例使用成员变量?是否避免涉及复制?

(注意:我对ASIO不满意,所以这里可能存在一个根本的误解)

c++ boost boost-asio

5
推荐指数
1
解决办法
259
查看次数

Boost Pool最大尺寸

我使用boost池作为静态内存提供程序,

void func()
{
  std::vector<int, boost::pool_allocator<int> > v;
  for (int i = 0; i < 10000; ++i)
    v.push_back(13);
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我们如何修复池的大小,我的意思是我们知道boost :: pool提供了一个静态内存分配器,但是我无法修复这个池的大小,它不断增长,应该有办法限制其规模.例如,我只想要一个200块的池,所以我可以采取200块之后,它应该虽然为NULL请让我现在如何做到这一点

c++ boost boost-pool

5
推荐指数
1
解决办法
1756
查看次数

针对 Boost.Log 的 g++ 静态链接错误

静态链接 Boost.Log 时出现链接错误。编译器版本是g++ 4.8.1。Boost版本是1.55.0。我可以毫无问题地进行动态链接(在 make 文件中使用“-DBOOST_LOG_DYN_LINK”),但是当我进行静态链接时,会出现链接错误。在其他 boost 库上不会出现此问题。我简化了代码以轻松重现此问题:

#include <boost/log/trivial.hpp>
int main()
{
    BOOST_LOG_TRIVIAL(trace) << "test trace";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

生成链接错误的构建命令:

g++ test.cpp -lboost_thread -lboost_log
Run Code Online (Sandbox Code Playgroud)

但是,如果我明确指定 lib 文件,就不会有问题:

g++ test.cpp -lboost_thread /usr/local/lib/libboost_log.a
Run Code Online (Sandbox Code Playgroud)

我还尝试添加 -L 选项并得到相同的错误:

g++ test.cpp -L/usr/local/lib -lboost_thread -lboost_log
Run Code Online (Sandbox Code Playgroud)

这是错误日志:

test.cpp:(.text+0x29): undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'
test.cpp:(.text+0x70): undefined reference to `boost::log::v2s_mt_posix::trivial::logger::get()'
/tmp/ccDUZ2gm.o: In function `boost::log::v2s_mt_posix::record::reset()':
test.cpp:(.text._ZN5boost3log12v2s_mt_posix6record5resetEv[_ZN5boost3log12v2s_mt_posix6record5resetEv]+0x23): undefined reference to `boost::log::v2s_mt_posix::record_view::public_data::destroy(boost::log::v2s_mt_posix::record_view::public_data const*)'
/tmp/ccDUZ2gm.o: In function `boost::log::v2s_mt_posix::record boost::log::v2s_mt_posix::sources::basic_composite_logger<char, boost::log::v2s_mt_posix::sources::severity_logger_mt<boost::log::v2s_mt_posix::trivial::severity_level>, boost::log::v2s_mt_posix::sources::multi_thread_model<boost::log::v2s_mt_posix::aux::light_rw_mutex>, boost::log::v2s_mt_posix::sources::features<boost::log::v2s_mt_posix::sources::severity<boost::log::v2s_mt_posix::trivial::severity_level>, void, void, void, void, void, void, void, void, void> >::open_record<boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, boost::log::v2s_mt_posix::trivial::severity_level …
Run Code Online (Sandbox Code Playgroud)

c++ logging boost g++ boost-log

5
推荐指数
1
解决办法
6651
查看次数

std :: bind和完美转发

以下代码无法编译:

#include <functional>

template<class ...Args>
void invoke(Args&&... args)
{
}

template<class ...Args>
void bind_and_forward(Args&&... args)
{
    auto binder = std::bind(&invoke<Args...>, std::forward<Args>(args)...);
    binder();
}

int main()
{
    int a = 1;
    bind_and_forward(a, 2);
}
Run Code Online (Sandbox Code Playgroud)

如果我理解正确的,原因如下:std::bind会将它的参数,当binderoperator()叫,它通过了所有绑定的参数作为左值 -即使是那些进入者bind右值.但是invoke为原始参数进行了实例化,它无法接受binder传递它的尝试.

这个问题有什么解决方案吗?

c++ stdbind perfect-forwarding c++11

5
推荐指数
1
解决办法
1617
查看次数