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

  • Home
  • SEARCH
  • 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 8407067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:16:47+00:00 2026-06-09T23:16:47+00:00

I wonder why cbegin and cend were introduced in C++11? What are cases when

  • 0

I wonder why cbegin and cend were introduced in C++11?

What are cases when calling these methods makes a difference from const overloads of begin and end?

  • 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-09T23:16:48+00:00Added an answer on June 9, 2026 at 11:16 pm

    It’s quite simple. Say I have a vector:

    std::vector<int> vec;
    

    I fill it with some data. Then I want to get some iterators to it. Maybe pass them around. Maybe to std::for_each:

    std::for_each(vec.begin(), vec.end(), SomeFunctor());
    

    In C++03, SomeFunctor was free to be able to modify the parameter it gets. Sure, SomeFunctor could take its parameter by value or by const&, but there’s no way to ensure that it does. Not without doing something silly like this:

    const std::vector<int> &vec_ref = vec;
    std::for_each(vec_ref.begin(), vec_ref.end(), SomeFunctor());
    

    Now, we introduce cbegin/cend:

    std::for_each(vec.cbegin(), vec.cend(), SomeFunctor());
    

    Now, we have syntactic assurances that SomeFunctor cannot modify the elements of the vector (without a const-cast, of course). We explicitly get const_iterators, and therefore SomeFunctor::operator() will be called with const int &. If it takes it’s parameters as int &, C++ will issue a compiler error.


    C++17 has a more elegant solution to this problem: std::as_const. Well, at least it’s elegant when using range-based for:

    for(auto &item : std::as_const(vec))
    

    This simply returns a const& to the object it is provided.

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

Sidebar

Related Questions

I wonder if there is a way for me to SCP the file from
i wonder what is the best solution to send an email from my app.
I wonder, why do I get not full response from my server ( Win
I wonder if there is an equivalent to the MySQL-Query: SELECT COUNT(*) FROM users
I wonder how can I download/save/write a file from my server to a client's
Wonder what the difference between: static PROCESSWALK pProcess32First=(PROCESSWALK)GetProcAddress(hKernel,Process32First); ... pProcess32First(...); what is hKernel? Look
wonder why when I export my project to *.air and I install it on
wonder what's wrong <table id=tblDomainVersion> <tr> <td>Version</td> <td>No of sites</td> </tr> <tr> <td class=clsversion>1.25</td>
wonder whether someone can help me with the following one... I have a struct
wonder if someone could help me with a little problem. I have session values

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.