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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:46:01+00:00 2026-05-26T18:46:01+00:00

i ran across something similar to the below code snippet, which throws a compiler

  • 0

i ran across something similar to the below code snippet, which throws a compiler error because its using a const_iterator. is there a reason why vec.end() in std::copy does not implicitly get a const cast ?

int main(int argc, char* argv[]) {

    vector<int> vec;
    vec.push_back(20);
    vec.push_back(30);
    vector<int> copy_vec;
    vector<int>::const_iterator i = vec.begin();
    std::copy(i,vec.end(),back_inserter(copy_vec));
    cerr<<copy_vec.size()<<endl;
    return 0;
}
  • 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-26T18:46:02+00:00Added an answer on May 26, 2026 at 6:46 pm

    No need of std::copy, neither you need a pair of iterators.

    Just do this:

    vector<int> copy_vec(vec); //use the copy constructor!
    

    And you’re done!

    As for your code why it is giving error, is because the first iterator to std::copy is a const_iterator, but the second iterator is just iterator. Both has to be same type, but they are not, and because of that the template argument deduction fails for std::copy which is a function template.

    To understand this with an example, consider this simple code:

    template<typename T>
    void f(T a, T b) {}
    
    int main()
    {
        int a = 100;
        char b = 'A';
        f(a,b);
    }
    

    It is giving error (see ideone):

    prog.cpp:8: error: no matching function for call to ‘f(int&, char&)’
    

    It doesn’t compile because we’re relying on template argument deduction. Since the type of the first parameter and second parameter is exactly same in the function template but we call this function passing a (which is int) as first argument and b (which is char) as second argument, it cannot deduce T uniquely from arguments of different types! Please note that conversion is not considered during template argument deduction.

    However, if we do not rely on template argument deduction, and instead provide the template argument explicitly, then it would work (see ideone):

    f<int>(a,b);  //works!
    

    It works as there is no need to deduce T from function arguments!

    Similarly, if you provide the template argument for std::copy, then even your code would work (see ideone):

    std::copy<vector<int>::const_iterator>(i,vec.end(),back_inserter(copy_vec));
           //^^^^^^^^^^^^^^^^^^^^^^^^^^^^ explicitly provide template argument!
    

    It works because iterator can convert into const_iterator, but const_iterator cannot convert into iterator which means the following would give error (see ideone):

    std::copy<vector<int>::iterator>(i,vec.end(),back_inserter(copy_vec));
            //^^^^^^^^^^^^^^^^^^^^^ non-const iterator!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran across some code recently at work (recreated to be similar to what
I ran across this pattern in the code of a library I'm using. It
I ran across this code recently which is part of a template class: operator
I'm playing around with using Simple.Data for my next project, and ran across something
I recently ran across a set of code which instantiated local maps as following:
Ran across this line of code: FormsAuth = formsAuth ?? new FormsAuthenticationWrapper(); What do
I ran across the following code in Ely Greenfield's SuperImage from his Book component
I ran across this case of UnboundLocalError recently, which seems strange: import pprint def
I ran across a very strange line of code in a legacy Perl application.
I ran across this snippet in an internal web site, but I'm having trouble

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.