Initializing a static constant vector of vectors in Visual Studio 2012

I am trying to create a static const vector of constant vectors ints (there should be a better way to do this) in Visual Studio 2012, and I cannot figure out the correct syntax for initializing it. I believe that 2012 uses a C ++ version that initializers do not allow, but I donโ€™t know how else to accomplish what I want.

I tried the following in 2013 and seem to compile ok:

.h:

static const std::vector<const std::vector<int>> PartLibrary; 

.cpp

 const std::vector<const std::vector<int>> Parts::PartLibrary { std::vector<int> { 29434 }, // 1 std::vector<int> { 26322 }, // 2 ... } 

However, when I try the same in 2012, it leads to errors:

 Error 1 error C2470: 'PartLibrary' : looks like a function definition, but there is no parameter list; skipping apparent body 

How can I properly initialize this? Is there a more suitable data type that I can use? I just want my static class to have a constant vector of int vectors, so I can quickly read, but not change the values.

+5
source share
1 answer

In C ++, you cannot have std :: vector < const anything>, see for example here . Elements must be assignable.

In C ++ 98, you can try the following initialization scheme. The disadvantage of copying vectors from an array to a vector:

 const std::vector<int> vectors[2] = { std::vector<int> (1, 29434), // vector of one element std::vector<int> (1, 26322), // vector of one element }; const std::vector<std::vector<int> > /*Parts::*/PartLibrary (vectors+0, vectors+2); // space needed for C++98---------^ 
0
source

Source: https://habr.com/ru/post/1207564/


All Articles