Topic: A proposal to add global std::size and other useful


Author: Riccardo Marcangelo <ricky.65@hotmail.com>
Date: Fri, 21 Jun 2013 09:43:50 -0700 (PDT)
Raw View
------=_Part_680_15938693.1371833030500
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable



A common task in C++ is to determine the number of elements in a built-in C=
=20
array. This is usually achieved with a macro "hack" usually named something=
=20
like "ARRAYSIZE" or "countof". Such macros are commonplace. For example:
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))


The problem with such a macro is that if a pointer is passed, instead of a=
=20
built-in array, code will compile but yield an erroneous result with no=20
compiler warning or error. More advanced versions such as Microsoft's=20
"_countof" macro use extra trickery to issue a compile time error if a=20
pointer is passed to it.

It would be beneficial if there were a standard and safe way of obtaining=
=20
the number of elements in a built-in array.

In C++11 we can dispense with a macro and use a constexpr function template=
=20
to return the the number of elements in a built-in array at compile time.=
=20

In his "Notes on Programming" Alexander Stepanov writes - "I made size into=
=20
a member function in STL in an attempt to please the standard committee. I=
=20
knew that begin, end and size should be global functions but was not=20
willing to risk another fight with the committee." Thankfully std::begin=20
and std::end global functions were included in C++11 but unfortunately not=
=20
a global std::size.

It would be beneficial to unify containers which have a size member=20
function and built-in arrays by providing an overload for containers which=
=20
have a size() member function. This would enable programmers to simply=20
remember to use std::size to determine the number of elements for both=20
built-in arrays and containers with a size() member function.

Example:
#include <iostream>
#include <vector>
#include <array>

namespace example
{  =20
=20
template < class T, std::size_t N >
constexpr std::size_t size( const T (&array)[N] )

{

    return N;

}=20

//all standard (and other) containers which have a size() member function
template< class C >
constexpr auto size( const C & container ) -> decltype(container.size())

{

    return container.size();

}

}=20

int main()=20
{

   int builtin_one [7] =3D {1, 2, 3, 4, 5 , 6, 7};=20

   int builtin_two [example::size(builtin_one)];

   std::array <int, example::size(builtin_one)> arr;

   std::array <int, example::size(arr)> arr2;

   std::vector<int> vec (5, 51);   =20

   std::cout << example::size(builtin_one) << '\n';

   std::cout << example::size(builtin_two) << '\n';

   std::cout << example::size(arr) << '\n';

   std::cout << example::size(arr2) << '\n';

   std::cout << example::size(vec) << '\n';

   return 0;

}


In C++11 an alternative approach is to use std::extent. For example:
int array [10];
auto array_size =3D std::extent<decltype(array)>::value;


Disadvantages of this approach are that it is verbose and we lose the=20
opportunity to unify built-in arrays and containers.

In addition, It would be useful if we had the global functions std::front()=
=20
and std::back() and for completeness it is also worth adding global=20
versions of max_size() and empty(). Perhaps even a global std::at()=20
function to provide bounds checking for built-in arrays.

I suggest that header <utility> is probably the most suitable location for=
=20
the aforementioned functions.=20

Questions:

=B7         Are there any limitations/caveats of the above approach?

=B7         How should built-in multidimensional arrays be supported? I thi=
nk=20
that most users would expect the result to be the total number of elements=
=20
in the array (dim0*dim1*...*dimN), rather than the number of elements in=20
the most significant dimension.

=B7         As there can be no references to C++14 "Runtime-sized arrays", =
is=20
it correct that they cannot be supported here? The same problem exists with=
=20
std::begin and std::end for such arrays. Is there anything that can be done=
?


Kind regards

Riccardo Marcangelo

--=20

---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/.



------=_Part_680_15938693.1371833030500
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<p class=3D"MsoNormal">A common task in C++ is to determine the number of e=
lements
in a built-in C array. This is usually achieved with a macro "hack" usually
named something like "ARRAYSIZE" or "countof". Such macros
are commonplace. For example:</p><div class=3D"prettyprint" style=3D"backgr=
ound-color: rgb(250, 250, 250); border: 1px solid rgb(187, 187, 187); word-=
wrap: break-word;"><code class=3D"prettyprint"><div class=3D"subprettyprint=
"><span style=3D"color: #800;" class=3D"styled-by-prettify">#define</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> ARRAY_SIZE</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify">a</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">)</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">(</span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">sizeof</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
">a</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)/</spa=
n><span style=3D"color: #008;" class=3D"styled-by-prettify">sizeof</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">(*(</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify">a</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">)))</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"><br></span></div></code></div><p class=3D"=
MsoNormal"><br>The problem with such a macro is that if a pointer is passed=
, instead of a built-in array, code will compile but yield an erroneous res=
ult
with no compiler warning or error. More advanced versions such as Microsoft=
's "_countof" macro use extra trickery to issue a compile time error if a p=
ointer is passed
to it.</p><p class=3D"MsoNormal">It would be beneficial if there were a sta=
ndard and safe way
of obtaining the number of elements in a built-in array.</p><p class=3D"Mso=
Normal">In C++11 we can dispense with a macro and use a constexpr
function template to return the&nbsp;the number of elements in&nbsp;a built=
-in array at compile time. </p><p class=3D"MsoNormal">In his "Notes on Prog=
ramming" Alexander Stepanov
writes - "I made size into a member
function in STL in an attempt to please the standard committee. I knew that
begin, end and size should be global functions but was not willing to risk
another fight with the committee." Thankfully std::begin and std::end
global functions were included in C++11 but unfortunately not a global
std::size.</p><p class=3D"MsoNormal">It would be beneficial to unify contai=
ners which have a size
member function and built-in arrays by providing an overload for containers
which have a size() member function. This would enable programmers to simpl=
y
remember to use std::size to determine the number of elements for both buil=
t-in
arrays and containers with a size() member function.</p><p class=3D"MsoNorm=
al">Example:</p><div class=3D"prettyprint" style=3D"background-color: rgb(2=
50, 250, 250); border: 1px solid rgb(187, 187, 187); word-wrap: break-word;=
"><code class=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"=
color: #800;" class=3D"styled-by-prettify">#include</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #080=
;" class=3D"styled-by-prettify">&lt;iostream&gt;</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #800=
;" class=3D"styled-by-prettify">#include</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify"> </span><span style=3D"color: #080;" class=3D=
"styled-by-prettify">&lt;vector&gt;</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"><br></span><span style=3D"color: #800;" class=3D"s=
tyled-by-prettify">#include</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> </span><span style=3D"color: #080;" class=3D"styled-by-pr=
ettify">&lt;array&gt;</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"><br><br></span><span style=3D"color: #008;" class=3D"styled-by-p=
rettify">namespace</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> example<br></span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> &nbsp; <br>&nbsp;<br></span><span style=3D"color: #008;" class=3D"styled-=
by-prettify">template</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">&lt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </s=
pan><span style=3D"color: #008;" class=3D"styled-by-prettify">class</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> T</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> std</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">::</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify">size_t N </span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">&gt;</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"><br></span><span style=3D"color: #008;" class=3D"styled-=
by-prettify">constexpr</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> std</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">::</span><span style=3D"color: #000;" class=3D"styled-by-prettify">si=
ze_t size</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #008;" class=3D"styled-by-prettify">const</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> T </span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">(&amp;</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify">array</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">)[</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify">N</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">]</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
)</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br><=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"><br><br>&nbsp; &nbsp=
; </span><span style=3D"color: #008;" class=3D"styled-by-prettify">return</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"> N</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br><br></span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">}</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> <br><br></span><span style=3D"color:=
 #800;" class=3D"styled-by-prettify">//all standard (and other) containers =
which have a size() member function</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"><br></span><span style=3D"color: #008;" class=3D"s=
tyled-by-prettify">template</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">&lt;</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify=
">class</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> C =
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&gt;</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span=
 style=3D"color: #008;" class=3D"styled-by-prettify">constexpr</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"=
color: #008;" class=3D"styled-by-prettify">auto</span><span style=3D"color:=
 #000;" class=3D"styled-by-prettify"> size</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">const</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> C </span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">&amp;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> c=
ontainer </span><span style=3D"color: #660;" class=3D"styled-by-prettify">)=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">-&gt;</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"c=
olor: #008;" class=3D"styled-by-prettify">decltype</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify">container</span><span style=3D"color: #660;"=
 class=3D"styled-by-prettify">.</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify">size</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">())</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"><br><br></span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br=
><br>&nbsp; &nbsp; </span><span style=3D"color: #008;" class=3D"styled-by-p=
rettify">return</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"> container</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify">size=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">();</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br></span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">}</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br><br></span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">}</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> <br><br></span><span style=3D"color:=
 #008;" class=3D"styled-by-prettify">int</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify"> main</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">()</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> <br></span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"><br><br>&nbsp; &nbsp;</span><span style=3D"color: #008;" class=3D"styled=
-by-prettify">int</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"> builtin_one </span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">[</span><span style=3D"color: #066;" class=3D"styled-by-prettify"=
>7</span><span style=3D"color: #660;" class=3D"styled-by-prettify">]</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">{</span><span style=3D"color: #066;" class=
=3D"styled-by-prettify">1</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> </span><span style=3D"color: #066;" class=3D"styled-by-prettify">2</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #066;" class=3D"styled-by-prettify">3</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> </span><span style=3D"color: #066;" class=3D"=
styled-by-prettify">4</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> </span><span style=3D"color: #066;" class=3D"styled-by-prettify">5</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #066=
;" class=3D"styled-by-prettify">6</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> </span><span style=3D"color: #066;" class=3D"styled-by-pret=
tify">7</span><span style=3D"color: #660;" class=3D"styled-by-prettify">};<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <br><br>&n=
bsp; &nbsp;</span><span style=3D"color: #008;" class=3D"styled-by-prettify"=
>int</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> built=
in_two </span><span style=3D"color: #660;" class=3D"styled-by-prettify">[</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify">example</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify">size</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">builtin_one</span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">)];</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"><br><br>&nbsp; &nbsp;std</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">array </span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color: #008;" =
class=3D"styled-by-prettify">int</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> example</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify">size</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify">builtin_on=
e</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)&gt;</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> arr</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br><br>&nbsp; &nbsp;std</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span=
 style=3D"color: #000;" class=3D"styled-by-prettify">array </span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"=
color: #008;" class=3D"styled-by-prettify">int</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> example</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify">size</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
">arr</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)&gt;=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> arr2</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"><br><br>&nbsp; &nbsp;std=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify">vector</span><span=
 style=3D"color: #080;" class=3D"styled-by-prettify">&lt;int&gt;</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> vec </span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"col=
or: #066;" class=3D"styled-by-prettify">5</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #066;" class=3D"style=
d-by-prettify">51</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">);</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
&nbsp; &nbsp;<br><br>&nbsp; &nbsp;std</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify">cout </span><span style=3D"color: #660;" class=3D"styled=
-by-prettify">&lt;&lt;</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> example</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
">size</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify">builtin_one</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">)</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt;</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #080;" class=3D"styled-by-prettify">'\n'</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br><br>&nbsp; &nbsp;std</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify">cout </span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">&lt;&lt;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> example</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify">size</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
>builtin_two</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">)</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt;</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span styl=
e=3D"color: #080;" class=3D"styled-by-prettify">'\n'</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"><br><br>&nbsp; &nbsp;std</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">cout </span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">&lt;&lt;</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> example</span><span style=3D"color: #660;"=
 class=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">size</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify">arr</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">)</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt;</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span styl=
e=3D"color: #080;" class=3D"styled-by-prettify">'\n'</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"><br><br>&nbsp; &nbsp;std</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">cout </span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">&lt;&lt;</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> example</span><span style=3D"color: #660;"=
 class=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">size</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify">arr2</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">)</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt;</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span sty=
le=3D"color: #080;" class=3D"styled-by-prettify">'\n'</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"><br><br>&nbsp; &nbsp;std</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify">cout </span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">&lt;&lt;</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> example</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">size</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify">vec</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">)</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt;</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span styl=
e=3D"color: #080;" class=3D"styled-by-prettify">'\n'</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"><br><br>&nbsp; &nbsp;</span><span style=3D=
"color: #008;" class=3D"styled-by-prettify">return</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #066;=
" class=3D"styled-by-prettify">0</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br><br></span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">}</span></div></code></div><p class=3D"MsoNormal"><br></p><p c=
lass=3D"MsoNormal">In C++11 an alternative approach is to use std::extent. =
For
example:</p><div class=3D"prettyprint" style=3D"background-color: rgb(250, =
250, 250); border: 1px solid rgb(187, 187, 187); word-wrap: break-word;"><c=
ode class=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"colo=
r: #008;" class=3D"styled-by-prettify">int</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> array </span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">[</span><span style=3D"color: #066;" class=3D"=
styled-by-prettify">10</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">];</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"><br></span><span style=3D"color: #008;" class=3D"styled-by-prettify">au=
to</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> array_s=
ize </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> std</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify">extent</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">decltype</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify">array</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">)&gt;::</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify">value</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">;</span></div></code></div><p class=3D"MsoNormal"><br></p><=
p class=3D"MsoNormal">Disadvantages of this approach are that it is verbose=
 and we
lose the opportunity to unify built-in arrays and containers.<br></p><p cla=
ss=3D"MsoNormal">In addition, It would be useful if we had the global funct=
ions
std::front() and std::back() and for completeness it is also worth adding
global versions of max_size() and empty(). Perhaps even a global std::at()
function to provide bounds checking for built-in arrays.</p><p class=3D"Mso=
Normal">I suggest that header &lt;utility&gt; is probably the most
suitable location for the aforementioned functions. </p><p class=3D"MsoNorm=
al">Questions:</p><p class=3D"MsoListParagraphCxSpFirst" style=3D"text-inde=
nt:-18.0pt;mso-list:l0 level1 lfo1"><!--[if !supportLists]--><span style=3D=
"font-family:Symbol;mso-fareast-font-family:Symbol;mso-bidi-font-family:
Symbol">=B7<span style=3D"font-size: 7pt; line-height: normal; font-family:=
 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><!--[endif]-->Are there any limitations/caveats of the above
approach?</p><p class=3D"MsoListParagraphCxSpFirst" style=3D"text-indent:-1=
8.0pt;mso-list:l0 level1 lfo1"><span style=3D"text-indent: -18pt; font-fami=
ly: Symbol;">=B7<span style=3D"font-size: 7pt; line-height: normal; font-fa=
mily: 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><span style=3D"text-indent: -18pt;">How should built-in multi=
dimensional arrays be
supported? I think that most users would expect the result to be the total
number of elements in the array (dim0*dim1*...*dimN), rather than the numbe=
r of
elements in the most significant dimension.</span></p><p class=3D"MsoListPa=
ragraphCxSpLast" style=3D"text-indent:-18.0pt;mso-list:l0 level1 lfo1"><!--=
[if !supportLists]--><span style=3D"font-family:Symbol;mso-fareast-font-fam=
ily:Symbol;mso-bidi-font-family:
Symbol">=B7<span style=3D"font-size: 7pt; line-height: normal; font-family:=
 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span><!--[endif]-->As there can be no references to C++14 "Runtime=
-sized arrays", is it
correct that they cannot be supported here? The same problem exists with
std::begin and std::end for such arrays. Is there anything that can be done=
?</p><p class=3D"MsoNormal"><br></p><p class=3D"MsoNormal">Kind regards</p>=
<p class=3D"MsoNormal">











































































































</p><p class=3D"MsoNormal">Riccardo Marcangelo</p>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_680_15938693.1371833030500--

.


Author: Riccardo Marcangelo <ricky.65@hotmail.com>
Date: Mon, 8 Jul 2013 09:44:46 -0700 (PDT)
Raw View
------=_Part_167_7529000.1373301886325
Content-Type: text/plain; charset=ISO-8859-1

A common task in C++ is to determine the number of elements in a built-in C
array. This is usually achieved with a macro "hack" usually named something
like "ARRAYSIZE" or "countof".  Such macros are commonplace. For example:

#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))


The problem with such a macro is that if a pointer is passed instead of a
built-in array, code will compile but yield an erroneous result with no
compiler warning or error. More advanced versions such as Microsoft's
_countof macro use extra trickery to issue a compile time error if a
pointer is passed to it.

It would be beneficial if there were a standard and safe way of obtaining
the number of elements in a built-in array.

In C++11 we can dispense with a macro and use a constexpr function template
to return the number of elements in a built-in array at compile time.

In his "Notes on Programming" Alexander Stepanov writes  - "I made size
into a member function in STL in an attempt to please the standard
committee. I knew that begin, end and size should be global functions but
was not willing to risk another fight with the committee." Thankfully
std::begin and std::end global functions were included in C++11 but
unfortunately not a global std::size.

It would be beneficial to unify containers which have a size member
function and built-in arrays by providing an overload for containers which
have a size() member function. This would enable programmers to simply
remember to use std::size to determine the number of elements for both
built-in arrays and containers with a size() member function.

Example:
#include <iostream>
#include <vector>
#include <array>

namespace example
{

//built-in C arrays
template <typename T, std::size_t N>
constexpr std::size_t size( const T (&array)[N] )
{

    return N;

}

//all standard (and other) containers which have a size() member function
template <typename C>
constexpr auto size( const C & container ) -> decltype(container.size())
{

    return container.size();

}

}

int main()
{

    int builtin_one [10] = {1, 2, 3, 4, 5 , 6, 7};

    int builtin_two [example::size(builtin_one)];

    std::array <int, example::size(builtin_one)> arr;

    std::array <int, example::size(arr)> arr2;

    std::vector<int> vec (5, 51);



    std::cout << example::size(builtin_one) << '\n';

    std::cout << example::size(builtin_two) << '\n';

    std::cout << example::size(arr) << '\n';

    std::cout << example::size(arr2) << '\n';

    std::cout << example::size(vec) << '\n';



    return 0;

}


In C++11 an alternative approach is to use std::extent. For example:
int array [10];
auto array_size = std::extent<decltype(array)>::value;


Disadvantages of this approach are that it is verbose and we lose the
opportunity to unify built-in arrays and containers.

In addition, It would be useful if we had the global functions std::front()
and std::back() and for completeness it is also worth adding global
versions of max_size() and empty(). Perhaps even a global std::at()
function to provide bounds checking for built-in arrays.

I suggest that header <utility> is probably the most suitable location for
the aforementioned functions.

Questions


   - Are there any limitations/caveats of the above approach?
   -  How should built-in multidimensional arrays be supported? I think
   that most users would expect the result to be the total number of elements
   in the array (dim0*dim1*...*dimN), rather than the number of elements in
   the most significant dimension.
   -  As there can be no references to C++14 "Runtime-sized arrays", is it
   correct that they cannot be supported here? The same problem exists with
   std::begin and std::end for such arrays. Is there anything that can be done?

Kind regards

Riccardo Marcangelo

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.



------=_Part_167_7529000.1373301886325
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<span style=3D"line-height: 18px;">A common task in C++ is to determine the=
 number of elements
in a built-in C array. This is usually achieved with a macro "hack" usually
named something like "ARRAYSIZE" or "countof". &nbsp;</span><span style=3D"=
line-height: 18px;">Such macros are commonplace. For example:</span><div><s=
pan style=3D"line-height: 18px;"><br></span>

<div class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); b=
order: 1px solid rgb(187, 187, 187); word-wrap: break-word;"><code class=3D=
"prettyprint"><div class=3D"subprettyprint"><span style=3D"color: #800;" cl=
ass=3D"styled-by-prettify">#define</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> ARRAY_SIZE</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify">a</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">)</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><sp=
an style=3D"color: #008;" class=3D"styled-by-prettify">sizeof</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify">a</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">)/</span><span style=3D"color: #008;" clas=
s=3D"styled-by-prettify">sizeof</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">(*(</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify">a</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">)))</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><b=
r></span></div></code></div><p class=3D"MsoNormal"><br>The problem with suc=
h a macro is that if a pointer is passed
instead of a built-in array, code will compile but yield an erroneous resul=
t
with no compiler warning or error. More advanced versions such as Microsoft=
's _countof
macro use extra trickery to issue a compile time error if a pointer is pass=
ed
to it.</p>

<p class=3D"MsoNormal">It would be beneficial if there were a standard and =
safe way
of obtaining the number of elements in a built-in array.</p>

<p class=3D"MsoNormal">In C++11 we can dispense with a macro and use a cons=
texpr
function template to return the number of elements in a built-in array at
compile time. </p>

<p class=3D"MsoNormal">In his "Notes on Programming" Alexander Stepanov
writes &nbsp;- "I made size into a member
function in STL in an attempt to please the standard committee. I knew that
begin, end and size should be global functions but was not willing to risk
another fight with the committee." Thankfully std::begin and std::end
global functions were included in C++11 but unfortunately not a global
std::size.</p>

<p class=3D"MsoNormal">It would be beneficial to unify containers which hav=
e a size
member function and built-in arrays by providing an overload for containers
which have a size() member function. This would enable programmers to simpl=
y
remember to use std::size to determine the number of elements for both buil=
t-in
arrays and containers with a size() member function.</p>

<p class=3D"MsoNormal">Example:</p>

<div class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); b=
order: 1px solid rgb(187, 187, 187); word-wrap: break-word;"><code class=3D=
"prettyprint"><div class=3D"subprettyprint"><span style=3D"color: #800;" cl=
ass=3D"styled-by-prettify">#include</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> </span><span style=3D"color: #080;" class=3D"styl=
ed-by-prettify">&lt;iostream&gt;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br></span><span style=3D"color: #800;" class=3D"st=
yled-by-prettify">#include</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #080;" class=3D"styled-by-pre=
ttify">&lt;vector&gt;</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> <br></span><span style=3D"color: #800;" class=3D"styled-by-pret=
tify">#include</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> </span><span style=3D"color: #080;" class=3D"styled-by-prettify">&lt;a=
rray&gt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> &=
nbsp; <br><br></span><span style=3D"color: #008;" class=3D"styled-by-pretti=
fy">namespace</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> example <br></span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> &n=
bsp;<br><br></span><span style=3D"color: #800;" class=3D"styled-by-prettify=
">//built-in C arrays </span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"><br></span><span style=3D"color: #008;" class=3D"styled-by-pret=
tify">template</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;<=
/span><span style=3D"color: #008;" class=3D"styled-by-prettify">typename</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> T</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> std</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify">size_t N</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">&gt;</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> <br></span><span style=3D"color: #008;" class=3D"=
styled-by-prettify">constexpr</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> std</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify">size_t size</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </=
span><span style=3D"color: #008;" class=3D"styled-by-prettify">const</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"> T </span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">(&amp;</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">array</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">)[</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify">N</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">]</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">)</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <=
br></span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> <br><br>&nbsp; =
&nbsp; </span><span style=3D"color: #008;" class=3D"styled-by-prettify">ret=
urn</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> N</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> <br><br></span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">}</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> &nbsp; <br><br></span><span style=
=3D"color: #800;" class=3D"styled-by-prettify">//all standard (and other) c=
ontainers which have a size() member function </span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #008;"=
 class=3D"styled-by-prettify">template</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">&lt;</span><span style=3D"color: #008;" class=3D"styled-=
by-prettify">typename</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> C</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">&gt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <b=
r></span><span style=3D"color: #008;" class=3D"styled-by-prettify">constexp=
r</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><=
span style=3D"color: #008;" class=3D"styled-by-prettify">auto</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> size</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" =
class=3D"styled-by-prettify">const</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> C </span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">&amp;</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> container </span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">)</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">-&gt;=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #008;" class=3D"styled-by-prettify">decltype</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">container</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">size</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">())</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> <br></span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> <br><br>&nbsp; &nbsp; </span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">return</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> container</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">.</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify">size</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">();</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <=
br><br></span><span style=3D"color: #660;" class=3D"styled-by-prettify">}</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"> &nbsp; <br>=
<br></span><span style=3D"color: #660;" class=3D"styled-by-prettify">}</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> <br></span><sp=
an style=3D"font-family: Arial, Helvetica, sans-serif;"><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">&nbsp;</span></span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: =
#008;" class=3D"styled-by-prettify">int</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> main</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">()</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> <br></span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> <br><br>&nbsp; &nbsp; </span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">int</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> builtin_one </span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">[</span><span style=3D"color: #066;" class=3D"styled-by-prettify=
">10</span><span style=3D"color: #660;" class=3D"styled-by-prettify">]</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">{</span><span style=3D"color: #066;" cla=
ss=3D"styled-by-prettify">1</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> </span><span style=3D"color: #066;" class=3D"styled-by-prettify">2=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #066;" class=3D"styled-by-prettify">3</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> </span><span style=3D"color: #066;" class=3D"=
styled-by-prettify">4</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> </span><span style=3D"color: #066;" class=3D"styled-by-prettify">5</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #066=
;" class=3D"styled-by-prettify">6</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> </span><span style=3D"color: #066;" class=3D"styled-by-pret=
tify">7</span><span style=3D"color: #660;" class=3D"styled-by-prettify">};<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <br><br>&n=
bsp; &nbsp; </span><span style=3D"color: #008;" class=3D"styled-by-prettify=
">int</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> buil=
tin_two </span><span style=3D"color: #660;" class=3D"styled-by-prettify">[<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify">example</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span=
 style=3D"color: #000;" class=3D"styled-by-prettify">size</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">builtin_one</span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">)];</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> <br><br>&nbsp; &nbsp; std</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify">array </span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color: #008;"=
 class=3D"styled-by-prettify">int</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> example</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify">size</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify">builtin_on=
e</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)&gt;</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> arr</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> <br><br>&nbsp; &nbsp; std</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify">array </span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">int</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"> example</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify">size</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify">arr</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)=
&gt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> arr2<=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> <br><br>&nbsp; &nbs=
p; std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify">vector</span=
><span style=3D"color: #080;" class=3D"styled-by-prettify">&lt;int&gt;</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> vec </span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=
=3D"color: #066;" class=3D"styled-by-prettify">5</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> </span><span style=3D"color: #066;" class=3D"=
styled-by-prettify">51</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">);</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> <br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=
 &nbsp; <br><br>&nbsp; &nbsp; std</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify">cout </span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">&lt;&lt;</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> example</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">::</span><span style=3D"color: #000;" class=3D"styled-by-prettify">s=
ize</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify">builtin_one</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">)</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">&lt;&lt;</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #080;=
" class=3D"styled-by-prettify">'\n'</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> <br><br>&nbsp; &nbsp; std</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">cout </span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">&lt;&lt;</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> example</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify">size</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">builtin_=
two</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt;</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"colo=
r: #080;" class=3D"styled-by-prettify">'\n'</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> <br><br>&nbsp; &nbsp; std</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify">cout </span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">&lt;&lt;</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> example</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify">size</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
>arr</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt;</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"col=
or: #080;" class=3D"styled-by-prettify">'\n'</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> <br><br>&nbsp; &nbsp; std</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify">cout </span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">&lt;&lt;</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> example</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify">size</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
>arr2</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt;</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"co=
lor: #080;" class=3D"styled-by-prettify">'\n'</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> <br><br>&nbsp; &nbsp; std</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify">cout </span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">&lt;&lt;</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> example</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify">size</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
">vec</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">&lt;&lt;</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"co=
lor: #080;" class=3D"styled-by-prettify">'\n'</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> <br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br><=
br>&nbsp; &nbsp; </span><span style=3D"color: #008;" class=3D"styled-by-pre=
ttify">return</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> </span><span style=3D"color: #066;" class=3D"styled-by-prettify">0</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> <br><br></span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">}</span></div></code></di=
v><p class=3D"MsoNormal"><br></p>

<p class=3D"MsoNormal">In C++11 an alternative approach is to use std::exte=
nt. For
example:</p>

<div class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); b=
order: 1px solid rgb(187, 187, 187); word-wrap: break-word;"><code class=3D=
"prettyprint"><div class=3D"subprettyprint"><span style=3D"color: #008;" cl=
ass=3D"styled-by-prettify">int</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> array </span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">[</span><span style=3D"color: #066;" class=3D"styled-by-pr=
ettify">10</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
];</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></sp=
an><span style=3D"color: #008;" class=3D"styled-by-prettify">auto</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> array_size </span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> std</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify">extent</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color: #008;" cla=
ss=3D"styled-by-prettify">decltype</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify">array</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">)&gt;::</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify">value</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">;</span></div></code></div><p class=3D"MsoNormal"><br></p><p class=3D"Mso=
Normal">Disadvantages of this approach are that it is verbose and we
lose the opportunity to unify built-in arrays and containers.<br></p>

<p class=3D"MsoNormal">In addition, It would be useful if we had the global=
 functions
std::front() and std::back() and for completeness it is also worth adding
global versions of max_size() and empty(). Perhaps even a global std::at()
function to provide bounds checking for built-in arrays.</p>

<p class=3D"MsoNormal">I suggest that header &lt;utility&gt; is probably th=
e most
suitable location for the aforementioned functions. </p>

<p class=3D"MsoNormal">Questions</p><p class=3D"MsoNormal"></p><ul><li><spa=
n style=3D"text-indent: -18pt; line-height: 18px;">Are there any limitation=
s/caveats of the above
approach?</span><br></li><li><span style=3D"line-height: 18px; text-indent:=
 -18pt; font-family: Symbol;"><span style=3D"font-size: 7pt; line-height: n=
ormal; font-family: 'Times New Roman';">&nbsp;</span></span><span style=3D"=
line-height: 18px; text-indent: -18pt;">How should built-in multidimensiona=
l arrays be
supported? I think that most users would expect the result to be the total
number of elements in the array (dim0*dim1*...*dimN), rather than the numbe=
r of
elements in the most significant dimension.</span><br></li><li><span style=
=3D"line-height: 18px; text-indent: -18pt; font-family: Symbol;"><span styl=
e=3D"font-size: 7pt; line-height: normal; font-family: 'Times New Roman';">=
&nbsp;</span></span><span style=3D"line-height: 18px; text-indent: -18pt;">=
As there can be no references to</span><span style=3D"line-height: 18px; te=
xt-indent: -18pt;">&nbsp;</span><span style=3D"line-height: 18px; text-inde=
nt: -18pt;">C++14 "Runtime-sized arrays", is it
correct that they cannot be supported here? The same problem exists with
std::begin and std::end for such arrays. Is there anything that can be done=
?</span></li></ul><p></p>

<p class=3D"MsoNormal">Kind regards</p>

<p class=3D"MsoNormal">Riccardo Marcangelo</p></div>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_167_7529000.1373301886325--

.