Will std::array replace the array type?

R

Rui Maciel

Here's some food for thought: will std::array eventually replace array
typesin C++ programs? In other words, what reasons are there to keep using
array types when std::array is available?


Rui Maciel
 
S

Saeed Amrollahi

Here's some food for thought: will std::array eventually replace array
typesin C++ programs?  In other words, what reasons are there to keep using
array types when std::array is available?

Rui Maciel

Hi Rui
I can't find any good reason to not replace core language array with
std::array. Actually standard array is exactly the core array without
its problems:
array vs. std::vector
compile-time static size run-time variable size
automatic storage dynamic storage
no dynamic resize dynamic resize
size unaware size aware
pointer arithmetic random-access iterator
implicit decay name to ptr no implicit decay ...

atd::array tries to gather good features of both:
compile-time static size
automatic storage
no dynamic resize
size aware
random-access iterator
no implicit decay
standard array like array, has no memory overhead
beyond the the elements storage. For me standard array
is the best of both worlds.

At last, because I'm an advisory member of C++ standardization
committee
and I follow the proposals for new revision of C++ (coined C++1y),
there is proposal for Variable Length Array (VLA). At the time being
I don't know exactly what it is, but they like to add this feature to
Core language array.

HTH and thank your for your food
-- Saeed Amrollahi Boyouki
 
J

Juha Nieminen

Saeed Amrollahi said:
I can't find any good reason to not replace core language array with
std::array. Actually standard array is exactly the core array without
its problems

Actually there is one reason why C static arrays cannot just be outright
replaced with std::array (although, granted, it's the only reason I can
think of at the moment). There are many situations where it would be
highly inconvenient to not to be able to do this:

const char* const names[] = // or whatever type, that's irrelevant here
{
"some name",
"another name",
"yet another name"
};

(The size of that array can be resolved, of course, with the good old
C trick "sizeof(names) / sizeof(names[0])". This can be calculated into
a compile-time constant variable if so needed.)

This is especially true the larger the array is, and the more frequently
it's changed (which may happen quite a lot during the development of the
program).

(Yes, I use this trick *a lot* in my current programming job, and it's
very useful, so it's not like it's a rare niche feature.)
 
S

Saeed Amrollahi

Saeed Amrollahi said:
I can't find any good reason to not replace core language array with
std::array. Actually standard array is exactly the core array without
its problems

  Actually there is one reason why C static arrays cannot just be outright
replaced with std::array (although, granted, it's the only reason I can
think of at the moment). There are many situations where it would be
highly inconvenient to not to be able to do this:

  const char* const names[] = // or whatever type, that's irrelevant here
  {
      "some name",
      "another name",
      "yet another name"
  };

  (The size of that array can be resolved, of course, with the good old
C trick "sizeof(names) / sizeof(names[0])". This can be calculated into
a compile-time constant variable if so needed.)

  This is especially true the larger the array is, and the more frequently
it's changed (which may happen quite a lot during the development of the
program).

  (Yes, I use this trick *a lot* in my current programming job, and it's
very useful, so it's not like it's a rare niche feature.)

Except, the size trick, you can write above
definition with standard array:
const array<char* const, 10> a =
{"some name", "another name", "yet another name" };

Regards,
-- Saeed Amrollahi Boyouki
 
M

Marc

Juha said:
Saeed Amrollahi said:
I can't find any good reason to not replace core language array with
std::array. Actually standard array is exactly the core array without
its problems

Actually there is one reason why C static arrays cannot just be outright
replaced with std::array (although, granted, it's the only reason I can
think of at the moment). There are many situations where it would be
highly inconvenient to not to be able to do this:

const char* const names[] = // or whatever type, that's irrelevant here
{
"some name",
"another name",
"yet another name"
};

(The size of that array can be resolved, of course, with the good old
C trick "sizeof(names) / sizeof(names[0])". This can be calculated into
a compile-time constant variable if so needed.)

This is especially true the larger the array is, and the more frequently
it's changed (which may happen quite a lot during the development of the
program).

(Yes, I use this trick *a lot* in my current programming job, and it's
very useful, so it's not like it's a rare niche feature.)

#include <type_traits>
#include <array>
template<class T,class...U>
std::array<typename std::decay<T>::type,1+sizeof...(U)>
make_array(T&&t,U&&...u){
return {{std::forward<T>(t),std::forward<U>(u)...}};
}
const auto names=make_array("a","b","c");

Well, this is not such a nice make_array, you'll find better ones on
the net, and there are talks about adding one to the standard, but it
shows how you can still avoid specifying the size parameter.
 
J

Juha Nieminen

Saeed Amrollahi said:
Except, the size trick, you can write above
definition with standard array:
const array<char* const, 10> a =
{"some name", "another name", "yet another name" };

Except that that array would incorrectly claim that there are 10
elements when there are only three. (Well, there *are* 10 elements
in the array. However, that's not the amount of *intended* elements,
which is the amount explicitly written, ie. three.)
 
J

Juha Nieminen

Marc said:
#include <type_traits>
#include <array>
template<class T,class...U>
std::array<typename std::decay<T>::type,1+sizeof...(U)>
make_array(T&&t,U&&...u){
return {{std::forward<T>(t),std::forward<U>(u)...}};
}
const auto names=make_array("a","b","c");

Well, this is not such a nice make_array, you'll find better ones on
the net, and there are talks about adding one to the standard, but it
shows how you can still avoid specifying the size parameter.

Seems a bit complicated for such a feature.

Also, will the created array be a compile-time constant? Well, I suppose
it's probably possible to put enough 'constexpr' keywords in there to make
it so.

(While it's relatively rare to need the array elements to be compile-time
constants, the size of the array is sometimes useful as such.)
 
J

Jorgen Grahn

Saeed Amrollahi said:
I can't find any good reason to not replace core language array with
std::array. Actually standard array is exactly the core array without
its problems

Actually there is one reason why C static arrays cannot just be outright
replaced with std::array (although, granted, it's the only reason I can
think of at the moment). There are many situations where it would be
highly inconvenient to not to be able to do this:

const char* const names[] = // or whatever type, that's irrelevant here
{
"some name",
"another name",
"yet another name"
};

(The size of that array can be resolved, of course, with the good old
C trick "sizeof(names) / sizeof(names[0])". This can be calculated into
a compile-time constant variable if so needed.)

This is especially true the larger the array is, and the more frequently
it's changed (which may happen quite a lot during the development of the
program).

(Yes, I use this trick *a lot* in my current programming job, and it's
very useful, so it's not like it's a rare niche feature.)

It's indeed useful and I use it a lot, especially in C. But I
sometimes worry about happens if someone replaces the array with a
pointer. 'sizeof ptr/sizeof ptr[0]' is usually a rather small value
....

/Jorgen
 
S

Saeed Amrollahi

  Except that that array would incorrectly claim that there are 10
elements when there are only three. (Well, there *are* 10 elements
in the array. However, that's not the amount of *intended* elements,
which is the amount explicitly written, ie. three.)

Sorry. I meant:
const array<char* const, 3> a =
{"some name", "another name", "yet another name" };

Thanks,
-- Saeed Amrollahi Boyouki
 
M

Marc

Juha said:
Seems a bit complicated for such a feature.

Once make_array is there, using it doesn't seem any harder than using
a C array...
Also, will the created array be a compile-time constant? Well, I suppose
it's probably possible to put enough 'constexpr' keywords in there to make
it so.

Yes, just one constexpr on the declaration of make_array, and one on
names.
(While it's relatively rare to need the array elements to be compile-time
constants, the size of the array is sometimes useful as such.)

Even without constexpr the size will be a compile-time constant, it is
part of the type...
 
J

Juha Nieminen

Saeed Amrollahi said:
Sorry. I meant:
const array<char* const, 3> a =
{"some name", "another name", "yet another name" };

Then you missed my point entirely (and your mistake actually
demonstrated superbly one of the problems).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,352
Latest member
DianeKulik

Latest Threads

Top