简体   繁体   English

从 C++ 中的函数返回数据的替代方法

[英]Alternative to returning data from functions in C++

I have a C++ program that takes in data using a ZMQ socket and processes it using many functions/submodules.我有一个 C++ 程序,它使用ZMQ套接字接收数据并使用许多函数/子模块处理它。

This is what I intend to do:这就是我打算做的:

在此处输入图像描述

Such that the results can be returned to the data provider using the same socket.这样可以使用相同的套接字将结果返回给数据提供者。

The problem is that as the results are computed in the last step but I don't want to return it using the same long pipeline;问题是,由于结果是在最后一步计算的,但我不想使用相同的管道返回它; there are already many call by references and values being used.已经有很多通过引用调用和使用的值。 Adding this would only complicate my system.添加这个只会使我的系统复杂化。

I could have used a global variable ( vector for instance) and then fill it with the results.我可以使用一个全局变量(例如vector ),然后用结果填充它。 main() could similarly access it and globally. main()可以类似地全局访问它。 Due to some limitations in my knowledge of zmq, I am only able to send simple data structures like an array and not STL structures.由于我对 zmq 知识的一些限制,我只能发送简单的数据结构,如数组,而不能发送 STL 结构。 But arrays size cannot be computed beforehand as it is result dependent.但是 arrays 大小不能预先计算,因为它取决于结果。 Hence, I cannot access it globally.因此,我无法在全球范围内访问它。

What alternatives do I have?我有什么选择?

I don't think chaining calls like that is beneficial.我不认为这样的链接调用是有益的。 Your pre-process() shouldn't think where it result goes;您的pre-process()不应该考虑结果的去向; it shouldn't know anything about infer() and call it;它不应该对infer()有任何了解并调用它; neither should infer() know about post-process() . infer()也不应该知道post-process() The regular approach is to use a "superstructure" that composites the calls.常规方法是使用组合调用的“上层结构”。

In the simplest version it will be something like:在最简单的版本中,它将类似于:

pipe += post_process( infer( pre_process( get_record( pipe ) ) ) ) ;

More likely, it will be a sequence with temporary storage of the intermediate results, so that you could peek into them with debugger and report progress.更有可能的是,这将是一个临时存储中间结果的序列,以便您可以使用调试器查看它们并报告进度。

raw_record = get_record( pipe );
pre_processed = pre_process( raw_record );
inference = infer( pre_processed );
result = post_process( inference );
pipe = combine_inputs( pipe, result );

In more advanced cases it will be some sort of dynamic queue of 'processor' functions, so that you can assemble it from various pre-processors, infer/analyze functions and post-processors, at run-time.在更高级的情况下,它将是某种“处理器”函数的动态队列,以便您可以在运行时从各种预处理器、推断/分析函数和后处理器组装它。

Each function takes its input, returns its output, and it's not its business what happens with it.每个 function 都接受它的输入,返回它的 output,这与它发生的事情无关。 You have a separate procedure that takes care of handling the correct data flow between them.您有一个单独的过程来处理它们之间的正确数据流。

I went with the cascaded returns as you all suggested.正如你们所建议的那样,我选择了级联回报。 As I had multiple variables to return, I used a struct to accommodate them and return together.由于我要返回多个变量,因此我使用了一个struct来容纳它们并一起返回。

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

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