Does the curly brace scope for clarifying code boundary increases code execution time? In my opinion, it does. Because exiting curly brace scope in C++ meaning stack unwinding and curly brace scope for comment purpose increases stack unwinding times. But I have no idea whether is expensive or not? Can I ignore the side effect?
You should focus on the code struture other than the code itself of the following code snippet.
#include <iostream>
#include <utility>
#include <vector>
#include <string>
int main()
{
std::string str = "Hello";
std::vector<std::string> v;
{// uses the push_back(const T&) overload, which means
// we'll incur the cost of copying str
v.push_back(str);
std::cout << "After copy, str is \"" << str << "\"\n";
//other code involves local variable
}
{// uses the rvalue reference push_back(T&&) overload,
// which means no strings will be copied; instead, the contents
// of str will be moved into the vector. This is less
// expensive, but also means str might now be empty.
v.push_back(std::move(str));
std::cout << "After move, str is \"" << str << "\"\n";
//other code involves local variable
}
std::cout << "The contents of the vector are \"" << v[0]
<< "\", \"" << v[1] << "\"\n";
}