Paw*_*zur 8 c++ protocol-buffers protobuf-c
我检查了移动构造函数规范和Message构造函数源,但没有找到.
如果没有,有没有人知道添加它的计划?
我正在使用proto3语法,编写库并考虑返回值与unique_ptr.
如果您尝试使用赋值运算符,RVO将执行优化以防止额外的副本.
// RVO will bring the return value to a without using copy constructor.
SomeMessage a = SomeFooWithMessageReturned();
Run Code Online (Sandbox Code Playgroud)如果要将std::move左值移动到列表/子消息等,请尝试使用ConcreteMessage::Swap方法.交换的项目将毫无用处.
// Non-copy usage.
somemessage.add_somerepeated_message()->Swap(&a);
somemessage.mutable_somesinglar_message()->Swap(&a);
// With message copying
somemessage.add_somerepeated_message()->CopyFrom(a);
*somemessage.mutable_somesinglar_message() = a;
Run Code Online (Sandbox Code Playgroud)从版本 2.6.1 开始,C++ protobuf 编译器仅生成复制构造函数和复制赋值运算符。但是,如果您的编译器支持返回值优化(并且满足其条件),则无论如何都不会调用复制构造函数。
您可以将一些打印语句添加到消息复制构造函数的生成代码中,以查看它们是否真正被调用。您还可以通过编写协议插件来完成此操作,以便它在协议调用之间保持不变。