I am always afraid of being hit for an initialization order here for such questions, but ...
#include <iostream> #include <vector> #include <iterator> struct A { static std::vector<int> s; }; static const int s_data[] = { 1,2,3 }; std::vector<int> A::s(std::begin(s_data), std::end(s_data)); int main() { std::copy(A::s.begin(), A::s.end(), std::ostream_iterator<int>(std::cout, " ")); return 0; }
Output
1 2 3
Just because you can doesnβt mean that you should = P
Winning the reward for the least effective way to do this:
#include <iostream> #include <vector> #include <cstdlib> using namespace std; template<typename T> std::vector<T> v_init(const T& t) { return std::vector<T>(1,t); } template<typename T, typename... Args> std::vector<T> v_init(T&& t, Args&&... args) { const T values[] = { t, args... }; std::vector<T> v1(std::begin(values), std::end(values)); return v1; } struct A { static std::vector<int> s; }; std::vector<int> A::s(v_init(1,2,3,4,5)); int main(int argc, const char *argv[]) { std::copy(A::s.begin(), A::s.end(), std::ostream_iterator<int>(std::cout, " ")); return 0; }
Output
1 2 3 4 5
This should hit at compile time if T and something in Args ... doesn't match type or type. Of course, if you have variations in template options, you also have initializer lists, but that makes brainwashing fun if nothing else.
source share