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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:01:07+00:00 2026-06-03T16:01:07+00:00

I am using the following struct as an input to STL’s generate_n algorithm: struct

  • 0

I am using the following struct as an input to STL’s generate_n algorithm:

struct GenerateNumber {     
    GenerateNumber () : i(0) {}
    int operator () (void) {        
        return  i++;
    }
private:
    int i;
};

An example to the code that uses this functor is:

std::vector <int> v1 (3);
std::vector <int> v2 (3);
GenerateNumber generateNumber;
std::generate_n (v1.begin (), 3, generateNumber);
std::generate_n (v2.begin (), 3, generateNumber);

However, the result is that both v1 and v2 contain {0,1,2}, instead of v2 to contain {3,4,5}. I have verified with a breakpoint that the GenerateNumber‘s constructor is called only once (I know it doesn’t make sense that the constructor is called more then once, but I checked it anyway).

I know I can solve this by making i static, but I don’t understand this behavior. How come the value of i is not kept between consecutive calls?

  • 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-06-03T16:01:09+00:00Added an answer on June 3, 2026 at 4:01 pm

    The generator object is copied when passed to generate_n. Try using std::ref, i.e.

    std::generate_n(v1.begin(), 3, std::ref(generateNumber));
    std::generate_n(v2.begin(), 3, std::ref(generateNumber));
    

    Edit: Note that std::ref is only available in C++11. It was introduced in TR1 as std::tr1::ref, and is also available in boost as boost::ref.

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

Sidebar

Related Questions

I'm using C++, and I have the following structures: struct ArrayOfThese { int a;
In the following: struct adt { void * A; }; int new_adt(const void *
Suppose I have the following: struct Person { std::string mName; Birthday mBirthday; }; using
I've been using the following code to create various struct, but only give people
Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct
I have the following code: #include <iostream> #include <vector> using namespace std; struct A{};
I decode video via libavcodec, using the following code: //Open input file if(avformat_open_input(&ctx, filename,
Given the following: struct Foo { int bar() const; }; struct IsEqual : public
Given the following: struct Foo { int bar() const; }; struct IsEqual : public
Consider the following: struct A { typedef int foo; }; struct B {}; template<class

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.