正在制作多少std :: string副本?

joe*_*els 1 c++ string

string str1("someString");
string str2 = string(str1);//how many copies are made here
    //copy2 = copy1?
Run Code Online (Sandbox Code Playgroud)

当您使用字符串(otherString)分配字符串时,是否会复制括号中的值,然后将该值复制到变量中?

Dav*_*eas 9

从技术上讲,可以有两个副本,但在现实生活中,你只做一个副本.也就是说,编译器将意识到不需要临时文件并优化掉副本.