开发者

Is there such a C++ optimisation?

开发者 https://www.devze.com 2022-12-09 14:52 出处:网络
E.g. vector<string> a; vector<string> b; a.push_back(\"first\"); b=a; Would it be optimised somehow as

E.g.

vector<string> a;

vector<string> b;

a.push_back("first");

b=a;

Would it be optimised somehow as

vector<string> b;

b.push_back("first");
开发者_开发知识库


Short answer: Yes.

Long answer: Not really an "optimization", as most modern compilers (read as: non-MSVC) will do that. It's called static single assignment (SSA) and GCC supports it since version 4.0 - and it kicks ass, too!


Well, you're saving yourself a call to operator=...

You should always remember the 2 rules of optimization though.

“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson

0

精彩评论

暂无评论...
验证码 换一张
取 消