In below code, how can i declare vv
to be const: vector<vector<float>> const vv;
? Eg. is there any c++0x version that will let me loop in the : ...
"initializer-list" section, before the {}
?
#include <vector>
using std::vector;
struct ST {
vector<int> const x; // simple constructor, initializ. via ": x(x)"
vector<vector<float>> vv; // requieres loop, can be done in ": ..."?
ST(vector<int> x, std::initializer_list<vector<float>> lv) : x(x) {
for (auto v : lv) {
vv.push_back(v);
}
}
};
std::vector
has a constructor that takes a initializer list, you do not need the loop:
struct ST {
vector<int> const x;
vector<vector<float>> const vv;
ST(vector<int> x, std::initializer_list<vector<float>> lv) :
x(x),
vv{lv}
{}
};
In case your example is oversimplified and you really need a loop, you can use a static
method to initialize const
members in the initializer list:
struct ST {
vector<int> const x;
vector<vector<float>> const vv;
ST(vector<int> x, std::initializer_list<vector<float>> lv) :
x(x),
vv{create_vector(lv)}
{}
static vector<vector<float>> create_vector(std::initializer_list<float> lv);
};