Start and end functions for built-in array types

I am trying to get the beginning and one of the final pointer for an array of C-style strings (an array of pointers to char). Why can't I call the begin and end function to get them?

#include <iostream>
#include <iterator>

int main(int argc,char *argv[])
{
  char **first=std::begin(argv),**last=std::end(argv);
}

The compiler says that there is no suitable function for my call begin(char**&)

+4
source share
2 answers

The problem the compiler is facing is that it has no way to guess the size of argv, because it is not an array, but a simple pointer.

, . ! , .:

int a[5];
int *p = a; // p[i] and a[i] are now the same
size_t s = sizeof(a); // s is 5 * sizeof(int) 20 on common (32 bits) architectures
size_t s2 = sizeof(p);  // s2 is just sizeof(int *) 4 on 32 bits architecture

, argv argc ( argv [argc] ). std::begin std::end . :

#include <iostream>
#include <iterator>

int main(int argc,char *argv[])
{
  char **first=argv,**last=argv + argc;
}
+1

. argv, char **, , , , .

#include <iostream>
#include <iterator>

int main(int argc,char *argv[])
{
   auto first = arg, last = argv + argc;
}

std:: begin std:: end, .

int MyMain( char * ( &argv )[10] )
{
   auto first = std::begin( argv ), last = std::end( argv );
}

, ,

   auto first = argv, last = argv + 10;

++

template <class T, size_t N> T* begin(T (&array)[N]);
4 Returns: array.

template <class T, size_t N> T* end(T (&array)[N]);
5 Returns: array + N.
+6

All Articles