0

I asked a similar question earlier without realizing that that wasn't quite specific enough.

So I have this function that has to take in all the arguments of a print function, with the ... and all, and then put it into a queue that will call the actual print function later.

Something like:

std::queue<SOMETHING> queue;
template <typename... Params>
void printLater(int a, int b, char* fmt, Params ...args) {
    queue.push(args);
}

template <typename... Params>
void print(int a, int b, char* fmt, Param ...args) {
    //whatever
}

void actuallyPrint() {
    //whatever
    print(queue.pop());
}

Context: I'm working with a piece of hardware that can only handle requests every 50ms or else they're ignored. My goal is to create a wrapper that will automatically add the delays if I send it a bunch at once.

My fallback if I cant do this, although I'd rather do this is just sprintf (or C++ equivalent) into a string only store the string in the queue and call print() without all the args.

  • 1
    I am not sure why you posted a new question instead of improving your other one. I voted the other one as duplicate of this one, since it is not specific enough to really be answered but is clearly intended to be the same question as here. – walnut Feb 02 '20 at 23:04
  • In these cases what you should have done is edit your first question including the additional information. Notice there's a link bellow the question saying:"edit", try clicking that. – bad_coder Feb 03 '20 at 00:52

1 Answers1

3

Something like this perhaps:

std::queue<std::function<void()>> queue;

template <typename... Params>
void printLater(int a, int b, char* fmt, Params ...args) {
    queue.push([=](){ print(a, b, fmt, args...); } );
}

void actuallyPrint() {
    queue.front()();
    queue.pop();
}
Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85