I could think of 3 cases, compiler could represent as a CSP and then optimize, however I don't know if it does. I don't assume a specific compiler optimization flag, however, to ensure optimization, you could assume -O2 or -O3 flags are given.
1) passing arguments themselves instead of copying, if the arguments are not used after a function call
void aFunction(std::string aStr);...std::string aString = makeAString(anIntegerInput);size_t mqSize = aString.size();aFunction(aString); // or a class method like aClass->aFunction(aString);std::cout << "Size : "<< mqSize << std::endl
since the aString is not used after aFunction call. It could logically infer that, instead of copying aString, does compiler move the string(aFunction's signature is not string&& - it is not a move operation here)?Would making aFunction's input parameter std::string &&aStr force it?
2) Here, a T object with default constructor is created, and then copy constructor used to initialize values of a Map.
template<typename T>void aMethod(std::map<std::string, T>& p, const std::vector<std::string>& aVec) { p.clear(); T t; for (auto it = aVec.begin(); it != aVec.end(); ++it) { p[*it] = t; }}
Compiler could detect that the "t" object is only default contructed object and instead of copying, may initialize values of the p map with default constructors.
3) We have a series of "theSameString"
std::map<std::string, int> temp{{"john", 5}, {"anna", 7}};int aValue = temp.find("john");int anotherValue = temp.find("john");int yetAnotherValue = temp.find("john");
here, does compiler create 4 different "john" const char* datastructures, or for each const char* to be created, checks previous const char* datastructures?Thank you,