Aftershock
Aftershock

Reputation: 5351

Is there such a C++ optimisation?

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");

Upvotes: 0

Views: 251

Answers (2)

Gab Royer
Gab Royer

Reputation: 9836

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

Upvotes: 1

LiraNuna
LiraNuna

Reputation: 67302

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!

Upvotes: 5

Related Questions