While trying to use brace initialization, a subtlety that can be found is when using std::vector
, like showed in the following sample:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
template <typename T>
void print(const char * msg, const vector<T>& v) {
cout << msg << endl;
cout << "Size: " << v.size() << endl;
for (size_t i = 0; i < v.size(); ++i) {
cout << "#" << (i+1) << ": " << v[i] << endl;
}
cout << "---------------------" << endl;
}
int main() {
vector<string> vs{3};
print("vector<string> vs{3};", vs);
vector<int> vi{3};
print("vector<int> vi{3};", vi);
}
The output is:
vector<string> vs{3}; Size: 3 #1: #2: #3: --------------------- vector<int> vi{3}; Size: 1 #1: 3 ---------------------
So, in the first case, the (so called...) uniform initialization initializes a vector conatining three empty strings, instead in the second case it initializes a vector containing just one integer having the value 3.
Besides this, are there other "gotchas" and subtleties to consider when using the new brace initialization style?