What is the best way to initialize a vector member object in C++11? Will the compiler optimize away the copy in foo or will bar be more efficient?
#include <vector> using namespace std; // C++11 style struct foo { vector<int> vec = vector<int>(256); }; // traditional struct bar { bar() : vec(256) {} vector<int> vec; };
vector::reservereserve), but only create the objects when you actually have them. (For example, default-initialization of a vector causes all the elements to be accessed, which may be unnecessary and wasteful.)std::dynarraybut it was taken out