Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1001751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:43:31+00:00 2026-05-16T07:43:31+00:00

I have a producer/consumers design in my application which implement Produce/Consume functions on user

  • 0

I have a producer/consumers design in my application which implement Produce/Consume functions on user types. But it doesn’t work very naturally with the standard library and especially with algorithms.

In C# there is the Enumerable and Observable concepts that can be used to easily implement stuff like this and get alot of nice free functionality.

In C++ there is the ios, istream, ostream, input_iterator, output_iterator concepts which I thought might be useful. But it seems to me that all of these are for primitive character types, e.g. char, int etc… and not for user types.

Sure I could use real functions such as Produce/Consumer and std::mem_fn for algorithms. But I was hoping there was a better way.

Im looking on some best-practice advice on how to go about designing i/o similar solutions on user types in C++.

E.g. from C#

class FrameProducer : IEnumerable<Frame> // pull frames
{...}

// Some engine between

class FrameConsumer : IObserver<Frame> // push frames
{...}

I was hoping for something similar in C++ e.g. which i dont believe is possible.

class FrameProducer : istream<Frame> // pull frames
{...}

// Some engine between

class FrameConsumer : ostream<Frame> // push frames
{...}

Maybe I’m thinking to hard about it and should just go by KISS.

Thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:43:31+00:00Added an answer on May 16, 2026 at 7:43 am

    Th terms are “insertion operator” and “extraction operator”, which insert and extract data from a stream.

    Here’s an example:

    #include <iostream>
    #include <sstream>
    
    struct foo
    {
        int x;
    };
    
    // insertion operator
    std::ostream& operator<<(std::ostream& s, const foo& f)
    {
        s << f.x; // insert a foo by inserting x
        return s;
    }
    
    // extraction operator
    std::istream& operator>>(std::istream& s, foo& f)
    {
        s >> f.x; // extract a foo by extracting x
        return s;
    }
    
    int main(void)
    {
        std::stringstream ss;
    
        foo f1 = {5};
        ss << f1;
    
        foo f2;
        ss >> f2;
    }
    

    Based on your desire to do:

    MyFrameProducer producer;
    MyFrameConsumer consumer;
    Frame frame; // frame should probably be in the while loop, since its 
    while(!producer.eof()) // lifetime doesn't need to exist outside the loop
    {
        producer >> frame;
        consumer << frame;
    } 
    

    You might make:

    struct MyFrameProducer {}; // add an eof function
    struct MyFrameConsumer {};
    struct Frame {};
    
    // producer produces a frame
    MyFrameProducer& operator>>(MyFrameProducer& p, Frame& f)
    {
        /* whatever it takes to make a frame */
    
        return p;
    }
    
    // consumer consumes a frame
    MyFrameConsumer& operator<<(MyFrameConsumer& c, const Frame& f)
    {
        /* whatever it takes to use a frame */
    
        return c;
    }
    

    Or something akin to it. (Sorry my understanding of the problem is small.) It is a bit weird to desire this interface, since it has nothing to do with streams, and you might be better off with a different interface (explicit methods).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to implement a multiple producers / multiple consumers example application for a
I have assignment to work on producer and consumer problem by use thread and
I have encountered a problem twice now whereby a producer thread produces N work
I have five consumers and one producer. The five consumers each output different data,
I have 3 threads: 2 consumers, ConsumerA and ConsumerB , and a Producer .
I have the following design: There is a Task which extends TimerTask and it
I want to implement a Producer - Consumers pattern using a ThreadPool for the
I have to write this produce consumer application using multithreading. I wrote the following
I have a C++ app in which I create pthreads to run user provided
I have a producer / consumer queue, except that there are specific types of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.