Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This is illegal right?

For a personal project I have been implementing my own libstdc++. Bit by bit, I've been making some nice progress. Usually, I will use examples from http://www.cplusplus.com/reference/ for some basic test cases to make sure that I have the obvious functionality working as expected.

Today I ran into an issue with std::basic_string::replace, specifically with the iterator based versions using the example copied verbatim from the site (http://www.cplusplus.com/reference/string/string/replace/) (I've added a comment to point out the lines in question):

// replacing in a string
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string base="this is a test string.";
  string str2="n example";
  string str3="sample phrase";
  string str4="useful.";

  // function versions used in the same order as described above:

  // Using positions:                 0123456789*123456789*12345
  string str=base;                // "this is a test string."
  str.replace(9,5,str2);          // "this is an example string."
  str.replace(19,6,str3,7,6);     // "this is an example phrase."
  str.replace(8,10,"just all",6); // "this is just a phrase."
  str.replace(8,6,"a short");     // "this is a short phrase."
  str.replace(22,1,3,'!');        // "this is a short phrase!!!"

  // Using iterators:                      0123456789*123456789*
  string::iterator it = str.begin();   //  ^
  str.replace(it,str.end()-3,str3);    // "sample phrase!!!"

  // *** this next line and most that follow are illegal right? ***

  str.replace(it,it+6,"replace it",7); // "replace phrase!!!"
  it+=8;                               //          ^
  str.replace(it,it+6,"is cool");      // "replace is cool!!!"
  str.replace(it+4,str.end()-4,4,'o'); // "replace is cooool!!!"
  it+=3;                               //             ^
  str.replace(it,str.end(),str4.begin(),str4.end());
                                       // "replace is useful."
  cout << str << endl;
  return 0;
}

In my version of replace is implemented in terms of a temporary string which I create then swap with *this. This clearly invalidates any iterators. So am I correct that the example is invalid? because it stores iterators, does a replace and then uses the iterators again?

My copy of the standard (ISO 14882:2003 - 21.3p5) says:

References, pointers, and iterators referring to the elements of a basic_string sequence may be invalidated by the following uses of that basic_string object:

- As an argument to non-member functions swap() (21.3.7.8), 
  operator>>() (21.3.7.9), and getline() (21.3.7.9).
- As an argument to basic_string::swap().
- Calling data() and c_str() member functions.
- Calling non-const member functions, except operator[](), at(),
  begin(), rbegin(),
  end(), and rend().
- Subsequent to any of the above uses except the forms of insert() and
  erase() which return iterators,
  the first call to non-const member functions operator[](), at(), begin(),
  rbegin(), end(), or rend().

The entry about non-const member functions seems to cover this. So unless I am missing something, then this code is using invalidated iterators right? Of course this code works just fine with gcc's libstdc++, but we all know that proves nothing as far as standards compliance.

like image 459
Evan Teran Avatar asked Jul 01 '10 19:07

Evan Teran


People also ask

What is the most common type of fair housing discrimination?

Of the 28,181 complaints of housing discrimination documented in 2016, 55 percent were based on disability. The figures come from a report released this week by the National Fair Housing Alliance, which analyzes government data and information collected by private, nonprofit fair housing groups.

What is the most a landlord can raise rent?

According to the Tenant Protection Act of 2019, also known as AB 1482, landlords are allowed annual rent increases of 5% plus the percentage change in the cost of living (Consumer Price Index) per year, up to 10%.

What makes an apartment illegal in NY?

New York City zones buildings to be residential or commercial and there are laws dictating specifications for bedroom size, heat, gas, water, and sanitation issues. So what makes an apartment illegal? For starters, the apartment is illegal if the space is used in a manner beyond what the building permit specifies.

Is it illegal to pay rent in cash UK?

Methods of payment Strictly speaking, payment should not be made by post unless the tenant is instructed to do so, and payment should be in cash. A tenant paying rent by cash should ask their landlord for a receipt of payments made in case there is a dispute.


1 Answers

This will appear to work if replace operates in-place. I don't think it's required to be implemented that way though. So yes, I would say your code is technically illegal.

like image 89
Michael Kristofik Avatar answered Oct 26 '22 19:10

Michael Kristofik