简体   繁体   English

msgpage C ++:使用MSGPACK_DEFINE发送原始指针

[英]msgpage C++ : send raw pointer with MSGPACK_DEFINE

I would like to send the following struct over msgpack. 我想通过msgpack发送以下结构。

struct MyStruct {
    std::string     name{""};
    int*            val{nullptr};

    MSGPACK_DEFINE( name, val );
};

Thus far in all of my projects, the only way I've streamed with msgpack is using MSGPACK_DEFINE, then writing the struct to msgpack::sbuffer (and sending it). 到目前为止,在我所有的项目中,我使用msgpack流式传输的唯一方法是使用MSGPACK_DEFINE,然后将结构写入msgpack :: sbuffer(并发送)。 the MSGPACK_DEFINE macro complains that that perhaps I missed the "->" so I'm guessing it doesn't detect that it's a pointer. MSGPACK_DEFINE宏抱怨说,也许我错过了“->”,所以我猜测它没有检测到它是一个指针。

Smart pointers seem to work though: 智能指针似乎可以工作:

struct MyStruct {
    std::string          name{""};
    std::shared_ptr<int> val{nullptr};

    MSGPACK_DEFINE( name, val );
};

The caveat is that the receiver on the other end needs val to be a raw pointer. 需要注意的是,另一端的接收器需要val作为原始指针。 I would like to do this without converting on the receiving side. 我想这样做而无需在接收端进行转换。 Any ideas? 有任何想法吗?

You failed to explain why you wish to do this. 您没有解释为什么要这样做。 Pointers are never meaningful when serialized (otherwise it is in-process data and there is no need to serialize). 指针在序列化时永远不会有意义(否则,它是进程中的数据,无需序列化)。

Just pass the value that the pointer points to. 只需传递指针指向的值即可。 If you need to represent "a number or NULL", then pass a struct containing an integer and boolean. 如果需要表示“数字或NULL”,则传递包含整数和布尔值的结构。

struct NullableInt {
    int value{0};
    bool null{true};
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM