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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:49:49+00:00 2026-06-11T23:49:49+00:00

The following code compiles just fine, overwriting the values in v2 with those from

  • 0

The following code compiles just fine, overwriting the values in v2 with those from v1:

std::vector<int> v1 = {1, 2, 3, 4, 5};
std::vector<int> v2 = {6, 7, 8, 9, 10};

std::copy(v1.begin(), v1.end(), v2.begin());

The third argument of std::copy is an OutputIterator. However, the Container requirements specify that a.begin(), where a is a Container object, should have a return type of iterator which is defined as:

any iterator category that meets the forward iterator requirements.

Forward iterator requirements do not include the requirements of output iterators, so is the example above undefined? I’m using the iterator as an output iterator even though there’s no obvious guarantee that it will be one.

I’m fairly certain the above code is valid, however, so my guess is that you can infer from the details about containers that the forward iterator returned by begin() will in fact also support the output iterator requirements. In that case, when does begin() not return an output iterator? Only when the container is const or are there other situations?

  • 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-11T23:49:50+00:00Added an answer on June 11, 2026 at 11:49 pm

    Forward iterators can conform the the specifications of an output iterator if they’re mutable, depending on the type of the sequence. It’s not explicitly spelled out (unlike the fact that they to input iterator requirements), but if we take a look at the requirements table

    Output iterator requirements

    we can go and check if a given forward iterator conforms to them:

    *r = o
    (§24.2.5/1): if X is a mutable iterator, reference is a reference to T

    A mutable reference is assignable (unless you have a non-assignable type, obviously).

    ++r, r++, *r++ = o
    (§24.2.5 Table 109)
    Forward iterator requirements

    The first line in Table 109 is the same requirement as for output iterators, except that forward iterators don’t have the remark. The second line is more restrictive than for output iterators, since it specifies that a reference must be returned.

    Bottom line, if you have a mutable forward iterator into a sequence of copy-assignable types, you have a valid output iterator.

    (Technically, a constant iterator into a sequence of types that have a operator=(...) const and mutable members would also qualify, but let’s hope nobody does something like that.)

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

Sidebar

Related Questions

The following line of code compiles just fine and behaves: list<const int *> int_pointers;
I had the following code line which compiles just fine under g++ and Visual
The following code is crashing my application on startup. It compiles just fine but
The following code snippet compiles just fine on Mac OS X with gcc, but
The following code works just fine: #include <exception> using namespace std; class FileException :
I came across the following code that compiles fine (using Visual Studio 2005): SomeObject
The following code does not compile with gcc 4.7 (20120114) , but compiles fine
The following code compiles fine on majority of compilers (include Microsoft Visual Studio's -
I’ve got the following code: #include <iostream> using namespace std; int main() { char*
I have the following code: typedef void VOID; int f(void); int g(VOID); which compiles

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.