g ++满足std :: string C ++ 11要求 [英] Does g++ meets std::string C++11 requirements

查看:95
本文介绍了g ++满足std :: string C ++ 11要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下示例:

int main()
{
    string x = "hello";
    //copy constructor has been called here.
    string y(x);
    //c_str return const char*, but this usage is quite popular.
    char* temp = (char*)y.c_str();

    temp[0] = 'p';

    cout << "x = " << x << endl;
    cout << "y = " << y << endl;

    cin >> x;
    return 0;
}

在visual studio编译器和g ++上运行它。
当我这样做时,我得到了两种不同的结果。
$ g $ b在g ++中:

Run it on visual studio compiler and on g++. When I did so, I got two different results.
in g++:

x = pello  
y = pello

在visual studio 2010中:

In visual studio 2010:

x = hello  
y = pello


$ b b

diff的原因很可能是g ++ std :: string实现使用COW(copy on write)技术,而visual studio不支持。

The reason for the diff is most likely that g++ std::string implementation uses COW (copy on write) techniques and visual studio does not.

现在C ++标准(第646页表64)关于字符串拷贝构造函数

Now the C++ standard (page 616 table 64) states with regards to string copy constructor

basic_string(const basic_string& str):

效果:

data()应该指向数组的第一个元素的第一个元素指向 str.data()

effects:
data() should "points at the first element of an allocated copy of the array whose first element is pointed at by str.data()"

含义COW是不允许的(至少对我的理解)。

怎么可能? >
g ++满足 std :: string C ++ 11要求?

Meaning COW is not allowed (at least to my understanding).
How can that be?
Does g++ meets std::string C++11 requirements?

在C ++ 11之前,这没有引起大问题,因为 c_str 没有返回指向实际的数据,字符串对象持有,所以改变它没有关系。但在更改后,COW +返回实际指针的组合可以破坏旧的应用程序(应用程序应该为坏的编码,但仍然)。

Before C++11 this did not pose a big problem since c_str didn't return a pointer to the actual data the string object holds, so changing it didn't matter. But after the change this combination of COW + returning the actual pointer can and breaks old applications (applications that deserve it for bad coding but nevertheless).

你同意我吗?如果是,可以做什么?任何人都有一个想法,如何去在一个非常大的旧代码环境(发条规则抓住这将是很好的)。

Do you agree with me? If yes, can something be done? Does anyone have an idea about how to go at it in a very big old code environments (a clockwork rule to catch this would be nice).

请注意,即使没有投射const const away,可能通过调用c_str导致指针无效,保存指针,然后调用非const方法(这将导致写操作)。

另一个没有强制转换constness的例子:

Note that even without casting the constness away, one might cause invalidation of a pointer by calling c_str, saving the pointer and then calling non-const method (which will cause write).
Another example without casting the constness away:

int main()
{
    string x = "hello";
    //copy constructor has been called here.
    string y(x);

    //y[0] = 'p';

    //c_str return const char*, but this usage is quite popular.
    const char* temp = y.c_str();

    y[0] = 'p';

    //Now we expect "pello" because the standart says the pointer points to the actual data
    //but we will get "hello"
    cout << "temp = " << temp << endl; 



    return 0;
}


推荐答案

a href =http://stackoverflow.com/q/12199710/560648>禁止COW 。但GCC 尚未更新其实施,据称为。一个新的实现,最终设计为取代 std :: string 实现,可以找到 ext / vstring.h

You're right that COW is disallowed. But GCC hasn't updated its implementation yet, allegedly due to ABI constraints. A new implementation, designed eventually to supplant the std::string implementation, can be found as ext/vstring.h.

中的错误libstdc ++ std :: string ,虽然不是这个,不会使它进入GCC 4.9; Jonathan在bug上指出它迄今为止只修复了 vstring

尽管如此,放弃 const ness然后突变常常是一个坏主意:虽然你是正确的,这应该在实践中是一个完全C ++ 11兼容的字符串实现安全,你在做假设,这个问题证明,你不能总是依靠这些假设来持有。因此,虽然您的代码示例可能是受欢迎的,但它在劣质代码中很流行,现在不应该写入。而且,当然,在C ++ 03中写的是无能为力的。

Despite all this, casting away constness then mutating is pretty much always a bad idea: though you're correct that this should in practice be safe with a fully C++11-compliant string implementation, you're making assumptions and this very problem proves that you cannot always rely on those assumptions to hold. So, while your code example may be "popular", it's popular in poor code, and shouldn't be written even now. And, of course, writing that in C++03 is flat-out incompetence!

这篇关于g ++满足std :: string C ++ 11要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆