1 #include <functional> 2 #include <stdlib.h> 3 4 template<typename ElemType> alloc(size_t count,std::function<ElemType (size_t)> get)5ElemType* alloc(size_t count, std::function<ElemType(size_t)> get) 6 { 7 ElemType *elems = new ElemType[count]; 8 for(size_t i = 0; i < count; i++) 9 elems[i] = get(i); 10 return elems; 11 } 12 main(int argc,const char * argv[])13int main (int argc, const char * argv[]) 14 { 15 int* data = alloc<int>(5, [] (size_t idx) -> int { 16 return 2 * idx + 1; 17 }); 18 return 0; // break here 19 } 20 21