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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:22:56+00:00 2026-05-18T08:22:56+00:00

Needless to over explain. The following code is self-evident: struct X { X(int n){}

  • 0

Needless to over explain. The following code is self-evident:

struct X
{
    X(int n){}
};

int main()
{
    std::vector<int> src;
    std::vector<X>   dest;

    // Below is not valid in current C++, but that is just what I want.
    transform(src.begin(), src.end(), back_insert(dest), std::bind(&X::X, _1)); 
}

A constructor takes some arguments and returns an object of the class of the constructor.

A constructor looks like a function, acts like a function, and is exactly a function.

So, I think std::bind should uniformly treat constructors and other callable objects.

However, how can I extend the function template “bind” to implement that?

  • 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-18T08:22:56+00:00Added an answer on May 18, 2026 at 8:22 am

    Since X is implicitly constructible from int, a simple copy should achieve the conversion.

    copy(src.begin(), src.end(), std::back_inserter(dest)); 
    

    Or even using the vector’s constructor:

    std::vector<X>   dest(src.begin(), src.end());
    

    In the general case, boost’s lambda library has a constructor functor.

    #include <boost/lambda/bind.hpp>
    #include <boost/lambda/construct.hpp>
    ...
    using namespace boost::lambda;
    transform(src.begin(), src.end(), std::back_inserter(dest), bind(constructor<X>(), _1));
    

    In this particular case binding might not be necessary, since there is only one argument to constructor.

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

Sidebar

Related Questions

I have found myself unable to explain why the following piece of code works.
I have a Rails application with over 2,000 examples in my RSpec tests. Needless
I'm working on a comparison website, so needless to say the search function is
I'm using the sortable function in jquery to sequence a faq list. Needless to
I'm coding a package for experimenting with evolutionary algorithms, and needless to say, it
sound like a terrible idea? Yeah I thought so too. Needless to say im
I'm (needless to say) relatively new to CSS3 and I'm having trouble with aligning
I just started using mock objects (using Java's mockito) in my tests recently. Needless
I am using this plugin (it's pretty cool). Needless to say, I am an
I am aware that Ini Files are meant for single lines of information, needless

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.