For example
struct A
{
static vector<int> s;
};
vector<int> A::s = {1, 2, 3};
However, my compiler doesn't support initialization list. Any way to implement it easily? Does lambda function help here?
For example
struct A
{
static vector<int> s;
};
vector<int> A::s = {1, 2, 3};
However, my compiler doesn't support initialization list. Any way to implement it easily? Does lambda function help here?
You can initialize an
std::vector
from two pointersYou can even factor this out in a template function:
Another idea:
Write a simple init function for the vector:
There's nothing particularly elegant. You can either copy the data from a static array, or initialise it with the result of a function call. The former might use more memory than you'd like, and the latter needs some slightly messy code.
Boost has a library to make that slightly less ugly:
Yes, it can save you from having to name a function just to initialise the vector:
(Strictly speaking, this should have an explicit return type,
[]()->vector<int>
, since the lambda body contains more than just areturn
statement. Some compilers will accept my version, and I believe it will become standard in 2014.)I always fear being shot down for initialization ordering here for questions like this, but..
Output
Just because you can doesn't mean you should =P
Winning the award for the least efficient way to do this:
Output
This should puke at compile-time if T and anything in Args... is not type-compliant or type-castable. Of course, if you have variadic templates odds are you also have initializer lists, but it makes for fun brain-food if nothing else.