I am trying to use initializer_listto instantiate a class, but got a wired error.
When you try to create a `ClassB``variable using:
ClassB b = { { 1, {} }, { 2, {} };
A violation of access to memory will occur. However, if you change to:
ClassA a0, a1;
ClassB b = { { 1, a0 }, { 2, a1 } };
The error disappears.
I tried to compile VC 2013 (without update 1) and gcc-C ++ 4.8.1. Using gcc-C ++ 4.8.1 does not result in any runtime error. Is this a bug in VC?
Can anyone help confirm? Thank!
Below is the SSCCE:
#include <iostream>
#include <initializer_list>
#include <map>
#include <vector>
#include <memory>
using namespace std;
struct ClassA {
struct Data {
vector<int> vs;
};
unique_ptr<Data> d;
ClassA() : d(new Data()) {}
ClassA(const ClassA& a) : ClassA() {
*d = *(a.d);
}
};
struct ClassB {
ClassB(initializer_list<pair<const int, ClassA> > v) { as = v; }
map<int, ClassA> as;
};
int main() {
ClassA a0, a1;
ClassB b = { { 1, {} }, { 2, {} } };
return 0;
}
source
share