Brace initialization subtleties

2019-05-23 23:08发布

问题:

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?

回答1:

It seems that you already understand the greedy nature of initializer-list constructors for containers. For the other surprising "gotchas" see this Q&A where I got tripped by this

std::string{ 65, 'C' } // NOT 65 times 'C', but "AC" ('A' is 65th character).