std :: string x(x); [英] std::string x(x);

查看:181
本文介绍了std :: string x(x);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  std :: string x(x); 

这在我的编译器崩溃非常严重。这是否意味着我应该在我自己的复制构造函数中测试 this!=& that ,或者我可以假设没有客户端会这么愚蠢?

$初始化一个东西本身是未定义的行为,这可能甚至意味着一旦它被调用,你甚至不能后来检测到它。假设编译器检测到它并且没有为鼻子生成汇编,而不是调用你的副本构造函数?



在实践中,你可以假设客户端不是那么蠢,如果他们是他们的业务,调试它,并弄清楚。


std::string x(x);

This crashes very badly on my compiler. Does this mean I should test for this != &that in my own copy constructors, or can I assume that no client will ever be so stupid?

解决方案

Initializing something with itself is undefined behavior, which probably might even mean that once it is invoked you even can't detect it later. Suppose the compiler detects it and out of spite generates assembly for nasal demons, not a call to your copy constructor at all?

In practice, you can assume that the client is not that stupid, and if they are it is their business to debug it and figure it out.

这篇关于std :: string x(x);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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