简体   繁体   中英

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.

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. main() could similarly access it and globally. 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. But arrays size cannot be computed beforehand as it is result dependent. 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; it shouldn't know anything about infer() and call it; neither should infer() know about 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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