2010-07-01 31 views
11

Kişisel bir proje için kendi libstdC++ uygulamasını uyguluyorum. Biraz bit, güzel bir gelişme kaydettim. Genelde, bazı temel test durumları için, beklendiği gibi çalışan işlevselliğin olduğundan emin olmak için http://www.cplusplus.com/reference/'dan örnekler kullanacağım.Bu yasadışı değil mi?

Bugün özellikle (Söz satırları işaret etmek bir yorum ekledim ) sitesinde (http://www.cplusplus.com/reference/string/string/replace/) birebir kopyalanmış örneği kullanarak yineleyici tabanlı sürümleri ile, std::basic_string::replace bir sorun koştu:

// 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:    *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:     *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; 
} 

Değiştirme sürümümde, oluşturduğum geçici bir dize olarak uygulanan ve daha sonra *this ile değiştirin. Bu açıkça herhangi bir yineleyici geçersiz kılar. Yani bu örnek geçersiz mi? Yineleyicileri depolar, bir değiştirme yapar ve tekrar yineleyicileri kullanır?

standardının My kopyası (ISO 14882: 2003 - 21.3p5) der ki:

Kaynaklar, işaretçiler ve yineleyiciler

izleyerek geçersiz olabilecek bir basic_string dizisinin elemanlarına atıfta o basic_string nesnenin kullanır:

- 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(). 

const olmayan üye işlevleri hakkında girişi bu kapsayacak şekilde görünüyor. Yani bir şeyleri kaçırmadığım sürece, bu kod geçersizleştirilmiş yineleyicileri doğru kullanıyor? Tabii ki bu kod gcc'nin libstdC++'sı ile gayet iyi çalışıyor, ama hepimiz biliyoruz ki standartlara uyum konusunda hiçbir şey kanıtlamadık.

+13

Bu aslında yasa dışıdır ve FBI'yı zaten bilgilendirdim. Umarım iyi bir avukatın vardır. –

+0

Daha fazla cezaevi inşa etmeleri gerekecek, eep. –

+1

Bunu merak ediyorsanız - merak etme. cplusplus.com hatalarla doludur. –

cevap

2

replace yerinde çalışıyorsa, bu işe yarayacaktır. Bu şekilde uygulanmasının gerekli olduğunu sanmıyorum. Evet, kodunuzun teknik olarak yasa dışı olduğunu söyleyebilirim.