Consider following code:
#include <iostream>
#include <vector>
struct C {
std::vector<int> a;
std::string b;
bool c;
};
void printC(const C &c) {
// ...
}
int main() {
printC({
{ 1, 2, 3 },
"ehlo",
false
});
}
This works, because compiler can generate proper constructor for me. But if I change struct C to this:
struct C {
std::vector<int> a;
std::string b;
bool c;
C() {
c = false;
}
};
The printC call stops working because compiler stops generating appropriate constructor. I've tried to write myself a constructor using std::initializer_list but failed.
So the question is - How to write constructor that will make the above code compile and work again?
A std::initializer_list object is automatically constructed when: a braced-init-list is used to list-initialize an object, where the corresponding constructor accepts an std::initializer_list parameter.
std::initializer_list a braced-init-list is used in list-initialization, including function-call list initialization and assignment expressions. a braced-init-list is bound to auto, including in a ranged for loop.
Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon. Following is an example that uses the initializer list to initialize x and y of Point class.
There are two ways to initialize a class object: Using a parenthesized expression list. The compiler calls the constructor of the class using this list as the constructor's argument list. Using a single initialization value and the = operator.
I've tried to write myself a constructor using std::initializer_list but failed.
You don't need one. You just need a c'tor taking a vector, string and boolean:
C(std::vector<int> a, std::string b, bool c)
: a(std::move(a))
, b(std::move(b))
, c(c) {
}
Your code should now be well-formed again. Though now it incurs two move operations, while the original aggregate version could have initialized the elements of your object directly. It's something worth considering.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With