// C++ program to demonstratethe use of initializer_list as // return type. #include #include #include using namespace std; void getNumbers( initializer_list myList ) { for( int x1 : myList ) { cout << x1 << " " ; } cout << endl ; } /* list1.cpp:20:21: warning: returning temporary ‘initializer_list’ does not extend the lifetime of the underlying array [-Winit-list-lifetime] 20 | return { 1, 2, 4 } ; initializer_list getNumbers1( ) { // A temporary array is created that will be destroyed once //this function ends. return { 1, 2, 4 } ; } */ int main() { //initializer_list num = getNumbers( { 3, 2, 1} ) ; getNumbers( { 3, 2, 1} ) ; initializer_list i1{-3, -2, -1} ; //Can't access an element in the middle. //cout << i1[2] << endl ; //can iterate through the list. initializer_list::iterator iter1 = i1.begin() ; for( ; iter1 != i1.end() ; iter1++ ) cout << *iter1 << " " ; cout << endl ; //Can be used in a range-based loop for( int x1 : i1 ) cout << x1 << " " ; cout << endl ; vector v1( i1 ) ; return 0; }