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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:21:33+00:00 2026-06-06T20:21:33+00:00

If we consider a std::string implementation that uses reference counting, consider this scenario: int

  • 0

If we consider a std::string implementation that uses reference counting, consider this scenario:

int main()
{
  string english = "Hello";
  string german  = english; //refcnt = 2
  string german2 = german;

  /* L1 */ german[1] = 'a';
  /* L2 */ *(german2.begin() + 1) = 'A';

  cout << english << endl << german << endl << german2 << endl;
  return 0;
}

What happens in L1 and L2? Is the reference counting broken and a deep copy is performed? I think so, but my concern says that if that occurs, doing a simple:

cout << german[1] << endl; 

or a simple:

cout << *(german.begin()) << endl;

in non-const contexts would perform unnecessary deep copies. Am I right? How do the implementations deal with this detail?

  • 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-06T20:21:34+00:00Added an answer on June 6, 2026 at 8:21 pm

    You are correct, a copy would be made in all four examples (L1, L2, and the two below), even though for the latter two it’s unnecessary.

    Unfortunately when the non-const version of operator[] is called or a non-const iterator is dereferenced, there is no way for the implementation to tell whether or not the resulting non-const reference will be used to modify the object, so it has to play it safe and make a copy.

    C++11 added functions cbegin() and cend() to strings and other containers which return const iterators even if called on a non-const object. This helps alleviate the problem. I’m not aware of a comparable solution for operator[].

    Note: having operator[] or the iterator’s operator*() return a proxy type, as some of the other answerers suggested, is not really an option because it breaks container requirements, one of which is that these functions return actual references. (This is why everyone now agrees that vector<bool> is a mistake – it uses proxies in this way).

    (Of course, if you’re writing your own reference-counted class, there’s nothing stopping you from using proxy types to achieve this.)

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

Sidebar

Related Questions

consider this translation unit: #include <map> #include <string> int main() { std::map<std::string, std::size_t> mp;
Consider this example: #include <algorithm> #include <iostream> int main() { std::string str = abcde4fghijk4l5mnopqrs6t8uvwxyz;
Consider this example: #include <iostream> #include <string> #include <vector> #include <iterator> int main() {
consider this code snippet void make(int n) { std::string user_input; std::istringstream iss(user_input); char letter;
Consider this: std::vector<int*> v(1, 0); This compiles fine with VC++10 (no warnings even at
Consider this line: std::wcout << Hello World!; Is it OK to pass char* or
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
Consider the following piece of C++0x code: a_signal.connect([](int i) { if(boost::any_cast<std::string>(_buffer[i]) == foo) {
#include <iostream> typedef struct _person { std::string name; unsigned int age; }Person; int main()
Consider this program: #include <stdio.h> int main() { printf(%s\n, __FILE__); return 0; } Depending

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.