且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么在执行 vector.push_back 时会调用两次复制构造函数

更新时间:2022-02-07 06:46:24

发生这种情况是因为您使用的是旧编译器.对不起,如果这不是一个真正令人兴奋的结论.:-( 没有其他人可以复制这个问题,即使在较新的 Microsoft 编译器上也是如此.虽然 Richard Hodges 说编译器可以***复制它认为合适的副本是正确的,但现代编译器会尽力避免它们.在特定的在您列出的情况下,一个体面的编译器不应该复制,如果您担心性能,您应该考虑升级到更新的版本.

This is happening because you are using an old compiler. Sorry if that isn't a really exciting conclusion. :-( No one else can replicate the problem, even on newer Microsoft compilers. While Richard Hodges is correct when he says that a compiler is free to make copies as it sees fit, modern compilers do their best to avoid them. In the specific case you list, a decent compiler should not be making a copy, and if you are concerned about performance you should consider upgrading to a newer version.