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 6766125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:47:35+00:00 2026-05-26T14:47:35+00:00

I would like to know if its possible without having to define an extra

  • 0

I would like to know if its possible without having to define an extra class, if a lambda can be adapted to act as a sink.

For example we currently can do the following:

std::vector<int> ilst;

std::copy(ilst.begin(),ilst.end(),std::ostream_iterator<int>(std::cout,"\n"));

What if something like the following could be possible? obviously the following wont
compile atm:

std::copy(ilst.begin(),ilst.end(),
          [](const int& i)
          {
             std::cout << i << "\n"; 
          });

I’ve been able to get around this problem, by defining a function object that implements dereference and function operators and takes a lambda as a predicate.

However I was wondering if there is some kind of C++ voodoo that will allow for the above without the need for an extra intermediary class to be provided?

  • 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-26T14:47:36+00:00Added an answer on May 26, 2026 at 2:47 pm

    You cannot do this without an adapter object.

    std::copy takes an output iterator which conforms to the standard library output iterator requirements. A lambda function is an object that has an operator() that takes certain arguments and returns a value. These are two different kinds of things.

    If you have an interface that takes Y, but you have a X, the only way to reconcile this is to introduce a Z that converts X into Y.

    And since X and Y are objects, Z must therefore be an object that provides the Y interface, but internally converts it into an X. Z is commonly called an adapter object.

    There is no alternative “C++ voodoo” that’s going to change this. There is no “other solution”. You must use some kind of adapter. Whether it’s a temporary of a class type or a function that returns an instance of a class, this can only be resolved with an adapter object.


    Applied to this particular situation – X is a lambda, Y is an output iterator, and Z is a function_output_iterator:

    #include <boost/function_output_iterator.hpp>
    #include <boost/range/algorithm/copy.hpp>
    #include <iostream>
    #include <vector>
    
    int main()
    {
        std::vector<int> ilst;
        boost::copy(
            ilst,
            boost::make_function_output_iterator(
                [](int i) { std::cout << i << "\n"; }));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know if its possible to deploy java class files without
I would like to know if its possible to get the users location without
Hi I would like to know if its possible to do something similar to
I would like to know if its possible to add a view say textview
I would like to know if it's possible to call a inline function with
I would like to know if it's possible to pass a parameter to a
I don't know if it's possible to do this, but I would like the
I would like to know how the Cocoa Framework got its name. I know
i would like know some reference. I know i can googling it. but prefer
I would like to know if I can open 2 different diagrams using MS

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.