suppose we have following function:

void someFunction(int * araye){
 for (int i=0;i<5;i++)
  cout <<araye[i]<<' ';
 cout <<'\n';
}

can we pass an array to this function by following syntax, under upcoming c++0x standards? :

someFunction({1,2,3,4,5});

if that's true, will we even be able to use this syntax in any case in which, array elements are from POD types like below :

class Test{
 int adad1;
 int adad2;
};
void someFunction(Test * araye){
 for (int i=0;i<3;i++)
  cout <<araye[i].adad1<<'-'<<araye[i].adad2<<' ';
 cout <<'\n';
}
someFunction({{1,2},{3,4},{5,6}});

Tested it with g++ -std=c++0x and g++ -std=gnu++0x, in both cases:
error: cannot convert ‘<brace-enclosed initializer list>’ to ‘const int*’ for argument ‘1’ to ‘void someFunction(const int*)’

I added const because otherwise it would never work for sure.

> can we pass arrays to functions

You couldn't pass arrays to functions in C, and couldn't pass them in C++98 either.

And you won't be able to do that in C++0x too. However, in C++0x you could pass a std::initializer_list<> to a function;
that is what someFunction({{1,2},{3,4},{5,6}}); attempts to do.

#include <iostream>
#include <vector>

template< typename SEQ > void foobar( const SEQ& s )
{
    for( auto iter = s.begin() ; iter != s.end() ; ++iter )
         std::cout << *iter << ' ' ;
    std::cout << '\n' ;
}

template< typename T > void foobar( const std::initializer_list<T>& a )
{
    for( auto iter = a.begin() ; iter != a.end() ; ++iter )
         std::cout << *iter + 10 << ' ' ;
    std::cout << '\n' ;
}

struct A { int a ; int b ; } ;

struct B { B( int x ) : b(x) {} private: int b ; } ;

struct C
{
  A aa[2] ;
  B bbb[3] ;
  operator int() const { return aa[0].a ; }
} ;

int main()
{
    foobar( { 0, 1, 2, 3, 4, 5 } ) ;

    std::vector<int> seq{ 0, 1, 2, 3, 4, 5 } ;
    foobar( seq ) ;

    foobar( { C{ { {1,2}, {3,4} }, {5,6,7} },
              C{ { {7,8}, {9,0} }, {1,2,3} },
              C{ { {4,5}, {6,7} }, {8,9,0} } } ) ;
}

Edited 6 Years Ago by vijayan121: n/a

Comments
very nice!
This question has already been answered. Start a new discussion instead.