Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Brace initialization subtleties

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?

like image 930
Mr.C64 Avatar asked Mar 18 '26 01:03

Mr.C64


1 Answers

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).
like image 100
TemplateRex Avatar answered Mar 19 '26 15:03

TemplateRex