Topic: Generated initializer_list
Author: douglas.boffey@gmail.com
Date: Mon, 28 Apr 2014 03:32:59 -0700 (PDT)
Raw View
------=_Part_78_15875660.1398681179202
Content-Type: text/plain; charset=UTF-8
It appears to me that one feature that is missing from most languages is a
method to compile-time compute a sequence (e.g. an array, etc.) One way to
do this is a program (e.g. yacc) that generates a compilation unit. Surely
the time is now ripe, with C++11's initializer_lists and C++14's relaxed
rules for constexprs to be able to directly code the algorithm withing the
compilation unit.
Maybe something along the lines of:
*template*<*typename* ValueType, *typename* Object, *typename* Args...>
std::initializer_list<ValueType> make_initializer_list(Object object,
Args... args) {
initializer_list<ValueType> result {};
*while* (object.more())
result.push_back(object());
*return* result;
}
--
---
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_78_15875660.1398681179202
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><DIV>It appears to me that one feature that is missing fro=
m most languages is a method to compile-time compute a sequence (e.g. an ar=
ray, etc.) One way to do this is a program (e.g. yacc) that generates=
a compilation unit. Surely the time is now ripe, with C++11's initia=
lizer_lists and C++14's relaxed rules for constexprs to be able to directly=
code the algorithm withing the compilation unit.</DIV>
<DIV> </DIV>
<DIV>Maybe something along the lines of:</DIV>
<DIV> </DIV>
<DIV><FONT color=3D#9900ff><STRONG><FONT color=3D#9900ff>template</FONT></S=
TRONG><<STRONG><FONT color=3D#9900ff>typename</FONT></STRONG> ValueType,=
<STRONG><FONT color=3D#9900ff>typename</FONT></STRONG> Object, <STRONG><FO=
NT color=3D#9900ff>typename</FONT></STRONG> Args...></FONT></DIV>
<DIV><FONT color=3D#9900ff>std::initializer_list<ValueType> make_init=
ializer_list(Object object, Args... args) {</FONT></DIV>
<DIV><FONT color=3D#9900ff> initializer_list<ValueType> result =
{};</FONT></DIV>
<DIV><FONT color=3D#9900ff> <STRONG><FONT color=3D#9900ff>while</FONT=
></STRONG> (object.more())</FONT></DIV>
<DIV><FONT color=3D#9900ff> result.push_back(object());</=
FONT></DIV>
<DIV><FONT color=3D#9900ff> <STRONG><FONT color=3D#9900ff>return</FON=
T></STRONG> result;</FONT></DIV>
<DIV><FONT color=3D#9900ff>}</FONT></DIV>
<DIV><FONT color=3D#9900ff></FONT> </DIV></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<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 />
------=_Part_78_15875660.1398681179202--
.
Author: Ville Voutilainen <ville.voutilainen@gmail.com>
Date: Mon, 28 Apr 2014 13:35:41 +0300
Raw View
On 28 April 2014 13:32, <douglas.boffey@gmail.com> wrote:
> It appears to me that one feature that is missing from most languages is a
> method to compile-time compute a sequence (e.g. an array, etc.) One way to
> do this is a program (e.g. yacc) that generates a compilation unit. Surely
> the time is now ripe, with C++11's initializer_lists and C++14's relaxed
> rules for constexprs to be able to directly code the algorithm withing the
> compilation unit.
>
> Maybe something along the lines of:
>
> template<typename ValueType, typename Object, typename Args...>
> std::initializer_list<ValueType> make_initializer_list(Object object,
> Args... args) {
> initializer_list<ValueType> result {};
> while (object.more())
> result.push_back(object());
> return result;
> }
initializer_list is probably not the right tool, it is by-design immutable.
--
---
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/.
.
Author: Andrew Tomazos <andrewtomazos@gmail.com>
Date: Mon, 28 Apr 2014 13:01:29 +0200
Raw View
To represent a compile-time sequence of values of a given literal type
T you can use an array defined with constexpr:
constexpr T a[] = ...;
If the values differ in type you can use a class type of literal type:
constexpr struct
{
T1 v1;
T2 v2;
T3 v3;
} S = ...;
There are a few other options, but it isn't clear from your example
what the problem is with the above. In all cases you can write a
constexpr function that returns the above types (well, you need to
wrap the array in a struct to return it as usual). You can create a
mutable local variable of the type within the constexpr function, fill
it out, and return it:
There are some limitations on literal types that different people are
thinking about and working on, but it isn't clear which one (if any)
you are hitting upon.
On Mon, Apr 28, 2014 at 12:32 PM, <douglas.boffey@gmail.com> wrote:
> It appears to me that one feature that is missing from most languages is a
> method to compile-time compute a sequence (e.g. an array, etc.) One way to
> do this is a program (e.g. yacc) that generates a compilation unit. Surely
> the time is now ripe, with C++11's initializer_lists and C++14's relaxed
> rules for constexprs to be able to directly code the algorithm withing the
> compilation unit.
>
> Maybe something along the lines of:
>
> template<typename ValueType, typename Object, typename Args...>
> std::initializer_list<ValueType> make_initializer_list(Object object,
> Args... args) {
> initializer_list<ValueType> result {};
> while (object.more())
> result.push_back(object());
> return result;
> }
>
>
> --
>
> ---
> 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/.
--
---
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/.
.
Author: douglas.boffey@gmail.com
Date: Tue, 29 Apr 2014 02:53:34 -0700 (PDT)
Raw View
------=_Part_638_15144896.1398765214779
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
>
> To represent a compile-time sequence of values of a given literal type=20
>> T you can use an array defined with constexpr:=20
>>
>> constexpr T a[] =3D ...;=20
>>
> The problem with that is that the array is a list of literals=20
(or constexprs). There should be a method to generate the list from an=20
algorithm, and have the generated list stored in the object file. As far=
=20
as I can tell, that facility is not available, either in core, nor in the=
=20
library.
I would like to type something along the lines of:
=20
constexpr auto primes[5] =3D get_nth_prime;
=20
given a function
=20
constexpr unsigned int get_nth_prime(size_t n)
=20
which would be equivalent to
=20
constexpr unsigned int primes[] =3D {2, 3, 5, 7, 11};
=20
> If the values differ in type you can use a class type of literal type:=20
>>
>> constexpr struct=20
>> {=20
>> T1 v1;=20
>> T2 v2;=20
>> T3 v3;=20
>> } S =3D ...;=20
>>
>> There are a few other options, but it isn't clear from your example=20
>> what the problem is with the above. In all cases you can write a=20
>> constexpr function that returns the above types (well, you need to=20
>> wrap the array in a struct to return it as usual). You can create a=20
>> mutable local variable of the type within the constexpr function, fill=
=20
>> it out, and return it:=20
>>
>> There are some limitations on literal types that different people are=20
>> thinking about and working on, but it isn't clear which one (if any)=20
>> you are hitting upon.=20
>
>
> This again misses the point=E2=80=94I=E2=80=99m not talking about aggrega=
tes of different=20
types. I am sorry if my example was not clear.
> =20
--=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_638_15144896.1398765214779
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><BLOCKQUOTE style=3D"BORDER-LEFT: #ccc 1px solid; MARGIN: =
0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class=3Dgmail_quote>
<BLOCKQUOTE style=3D"BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex=
; PADDING-LEFT: 1ex" class=3Dgmail_quote>To represent a compile-time sequen=
ce of values of a given literal type <BR>T you can use an array defined wit=
h constexpr: <BR><BR> constexpr T a[] =3D ...; <BR></BLOCKQUOT=
E></BLOCKQUOTE>
<DIV>The problem with that is that the array is a list of literals (or =
;constexprs). There should be a method to generate the list from an a=
lgorithm, and have the generated list stored in the object file. As f=
ar as I can tell, that facility is not available, either in core, nor in th=
e library.<BR></DIV>
<DIV>I would like to type something along the lines of:</DIV>
<DIV> </DIV>
<DIV><FONT color=3D#9900ff>constexpr auto primes[5] =3D get_nth_prime;</FON=
T></DIV>
<DIV> </DIV>
<DIV>given a function</DIV>
<DIV> </DIV>
<DIV><FONT color=3D#9900ff>constexpr unsigned int get_nth_prime(size_t=
n)</FONT></DIV>
<DIV> </DIV>
<DIV>which would be equivalent to</DIV>
<DIV> </DIV>
<DIV><FONT color=3D#9900ff>constexpr unsigned int primes[] =3D {2=
, 3, 5, 7, 11};</FONT></DIV>
<DIV> </DIV>
<BLOCKQUOTE style=3D"BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex=
; PADDING-LEFT: 1ex" class=3Dgmail_quote>
<BLOCKQUOTE style=3D"BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex=
; PADDING-LEFT: 1ex" class=3Dgmail_quote>If the values differ in type you c=
an use a class type of literal type: <BR><BR> constexpr struct=
<BR> { <BR> T1 v1; <BR> =
; T2 v2; <BR> T=
3 v3; <BR> } S =3D ...; <BR><BR>There are a few other options,=
but it isn't clear from your example <BR>what the problem is with the abov=
e. In all cases you can write a <BR>constexpr function that returns t=
he above types (well, you need to <BR>wrap the array in a struct to return =
it as usual). You can create a <BR>mutable local variable of the type=
within the constexpr function, fill <BR>it out, and return it: <BR><BR>The=
re are some limitations on literal types that different people are <BR>thin=
king about and working on, but it isn't clear which one (if any) <BR>you ar=
e hitting upon. </BLOCKQUOTE><BR></BLOCKQUOTE>
<DIV>This again misses the point=E2=80=94I=E2=80=99m not talking about aggr=
egates of different types. I am sorry if my example was not clear.</D=
IV>
<BLOCKQUOTE style=3D"BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex=
; PADDING-LEFT: 1ex" class=3Dgmail_quote> </BLOCKQUOTE></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<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 />
------=_Part_638_15144896.1398765214779--
.
Author: Andrew Tomazos <andrewtomazos@gmail.com>
Date: Tue, 29 Apr 2014 14:56:07 +0200
Raw View
> I would like to type something along the lines of:
>
> constexpr auto primes[5] = get_nth_prime;
>
> given a function
>
> constexpr unsigned int get_nth_prime(size_t n)
>
> which would be equivalent to
>
> constexpr unsigned int primes[] = {2, 3, 5, 7, 11};
Let us ignore prime sieves for the moment, and assume you have an
expensive function get_nth_prime. You can write the following I
think:
constexpr std::array<int, 5> cache_primes()
{
std::array<int, 5> A = {0};
for (size_t i = 0; i < 5; i++)
A[i] = get_nth_primes(i);
return A;
}
constexpr std:array<int, 5> primes = cache_primes();
cache_primes is called during translation (at compile-time), the
primes array is filed out and (assuming odr-used) is stored in the
object file like you want.
Is that what you mean?
--
---
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/.
.
Author: douglas.boffey@gmail.com
Date: Thu, 1 May 2014 02:52:45 -0700 (PDT)
Raw View
------=_Part_3726_31111225.1398937965208
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
On Tuesday, 29 April 2014 13:56:07 UTC+1, Andrew Tomazos wrote:=20
>
> > I would like to type something along the lines of:=20
> >=20
> > constexpr auto primes[5] =3D get_nth_prime;=20
> >=20
> > given a function=20
> >=20
> > constexpr unsigned int get_nth_prime(size_t n)=20
> >=20
> > which would be equivalent to=20
> >=20
> > constexpr unsigned int primes[] =3D {2, 3, 5, 7, 11};=20
>
> Let us ignore prime sieves for the moment, and assume you have an=20
> expensive function get_nth_prime. You can write the following I=20
> think:=20
>
> constexpr std::array<int, 5> cache_primes()=20
> {=20
> std::array<int, 5> A =3D {0};=20
>
> for (size_t i =3D 0; i < 5; i++)=20
> A[i] =3D get_nth_primes(i);=20
>
> return A;=20
> }=20
>
> constexpr std:array<int, 5> primes =3D cache_primes();=20
>
> cache_primes is called during translation (at compile-time), the=20
> primes array is filed out and (assuming odr-used) is stored in the=20
> object file like you want.=20
>
> Is that what you mean?=20
>
=20
This goes part way to resolving the issue, but there are still two major=20
problems with that solution:
=20
a) the size of the array might not be known by the user=E2=80=94suppose, fo=
r=20
example, that I wanted to code tables for a LALR(1) parser (without the=20
need for a source code generator like yacc/byacc/bison), it would be=20
unreasonable for the system to expect the programmer to know the array=20
sizes, and
b) this constrains the user to use a std::array=E2=80=A6 what if it were mo=
re=20
appropriate for a different data structure?
=20
After thinking about it, it seems that the best way to resolve such=20
deficiencies would be to add the necessary ctors to std::initializer_list,=
=20
e.g.
=20
template<typename ValueType>
std::initializer_list<ValueType>::initializer_list(size_t members,=20
ValueType (*fn)(size_t));
=20
which would be equivalent to {fn[0], fn[1], =E2=80=A6, fn[members =E2=80=93=
1]}
=20
template<typename ValueType, typename Data>
std::initializer_list<ValueType>::initializer_list(ValueType=20
(*next_datum)(const Data data), Data (*iterate)(const Data data), bool=20
(*more)(const Data data), const Data &initial_data)
=20
which would be equivalent to {next_datum(initial_data),=20
next_datum(iterate(initial_data)),=20
next_datum(iterate(iterate(initial_data))), =E2=80=A6}, continuing while=20
more(iterate(=E2=80=A6(initial_data)=E2=80=A6).
=20
I cannot see a way of using a ctor to generate the initializer_list from an=
=20
object, since it would need to be generated within the ctor to be mutable.
--=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_3726_31111225.1398937965208
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><BR>On Tuesday, 29 April 2014 13:56:07 UTC+1, Andrew Tomaz=
os wrote:=20
<BLOCKQUOTE style=3D"BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex=
; PADDING-LEFT: 1ex" class=3Dgmail_quote>> I would like to type somethin=
g along the lines of: <BR>> <BR>> constexpr auto primes[5] =3D get_nt=
h_prime; <BR>> <BR>> given a function <BR>> <BR>> constexpr uns=
igned int get_nth_prime(size_t n) <BR>> <BR>> which would be equivale=
nt to <BR>> <BR>> constexpr unsigned int primes[] =3D {2, 3, 5, 7, 11=
}; <BR><BR>Let us ignore prime sieves for the moment, and assume you have a=
n <BR>expensive function get_nth_prime. You can write the following I=
<BR>think: <BR><BR> constexpr std::array<int, 5> cache_=
primes() <BR> { <BR> std::array<=
int, 5> A =3D {0}; <BR><BR> for (size_t i =3D=
0; i < 5; i++) <BR> A[i]=
=3D get_nth_primes(i); <BR><BR> return A; <BR>&n=
bsp; } <BR><BR> constexpr std:array<int, 5> prime=
s =3D cache_primes(); <BR><BR>cache_primes is called during translation (at=
compile-time), the <BR>primes array is filed out and (assuming odr-used) i=
s stored in the <BR>object file like you want. <BR><BR>Is that what you mea=
n? <BR></BLOCKQUOTE>
<DIV> </DIV>
<DIV>This goes part way to resolving the issue, but there are still two maj=
or problems with that solution:</DIV>
<DIV> </DIV>
<DIV>a) the size of the array might not be known by the user=E2=80=94suppos=
e, for example, that I wanted to code tables for a LALR(1) parser (without =
the need for a source code generator like yacc/byacc/bison), it would be un=
reasonable for the system to expect the programmer to know the array sizes,=
and</DIV>
<DIV>b) this constrains the user to use a std::array=E2=80=A6 what if it we=
re more appropriate for a different data structure?</DIV>
<DIV> </DIV>
<DIV>After thinking about it, it seems that the best way to resolve such de=
ficiencies would be to add the necessary ctors to std::initializer_list, e.=
g.</DIV>
<DIV> </DIV>
<DIV><FONT color=3D#9900ff>template<typename ValueType></FONT></DIV>
<DIV><FONT color=3D#9900ff>std::initializer_list<ValueType>::initiali=
zer_list(size_t members, ValueType (*fn)(size_t));</FONT></DIV>
<DIV> </DIV>
<DIV>which would be equivalent to {fn[0], fn[1], =E2=80=A6, fn[members =E2=
=80=93 1]}</DIV>
<DIV> </DIV>
<DIV><FONT color=3D#9900ff>template<typename ValueType, typename Data>=
;</FONT></DIV>
<DIV><FONT color=3D#9900ff>std::initializer_list<ValueType>::initiali=
zer_list(ValueType (*next_datum)(const Data data), Data (*iterate)(const Da=
ta data), bool (*more)(const Data data), const Data &initial_data)</FON=
T></DIV>
<DIV> </DIV>
<DIV>which would be equivalent to {next_datum(initial_data), next_datum(ite=
rate(initial_data)), next_datum(iterate(iterate(initial_data))), =E2=80=A6}=
, continuing while more(iterate(=E2=80=A6(initial_data)=E2=80=A6).</DIV>
<DIV> </DIV>
<DIV>I cannot see a way of using a ctor to generate the initializer_list fr=
om an object, since it would need to be generated within the ctor to be mut=
able.</DIV></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<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 />
------=_Part_3726_31111225.1398937965208--
.
Author: Nicola Gigante <nicola.gigante@gmail.com>
Date: Thu, 1 May 2014 12:30:06 +0200
Raw View
--Apple-Mail-B0131C36-B5B1-4B02-B2EE-F3E730ABE301
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Il giorno 01/mag/2014, alle ore 11:52, douglas.boffey@gmail.com ha scritto:
>=20
>=20
>> On Tuesday, 29 April 2014 13:56:07 UTC+1, Andrew Tomazos wrote:
>> > I would like to type something along the lines of:=20
>> >=20
>> > constexpr auto primes[5] =3D get_nth_prime;=20
>> >=20
>> > given a function=20
>> >=20
>> > constexpr unsigned int get_nth_prime(size_t n)=20
>> >=20
>> > which would be equivalent to=20
>> >=20
>> > constexpr unsigned int primes[] =3D {2, 3, 5, 7, 11};=20
>>=20
>> Let us ignore prime sieves for the moment, and assume you have an=20
>> expensive function get_nth_prime. You can write the following I=20
>> think:=20
>>=20
>> constexpr std::array<int, 5> cache_primes()=20
>> {=20
>> std::array<int, 5> A =3D {0};=20
>>=20
>> for (size_t i =3D 0; i < 5; i++)=20
>> A[i] =3D get_nth_primes(i);=20
>>=20
>> return A;=20
>> }=20
>>=20
>> constexpr std:array<int, 5> primes =3D cache_primes();=20
>>=20
>> cache_primes is called during translation (at compile-time), the=20
>> primes array is filed out and (assuming odr-used) is stored in the=20
>> object file like you want.=20
>>=20
>> Is that what you mean?
> =20
> This goes part way to resolving the issue, but there are still two major =
problems with that solution:
> =20
> a) the size of the array might not be known by the user=E2=80=94suppose, =
for example, that I wanted to code tables for a LALR(1) parser (without the=
need for a source code generator like yacc/byacc/bison), it would be unrea=
sonable for the system to expect the programmer to know the array sizes, an=
d
In this specific case, the size of the table is easily computed ahead of ti=
me, so you can compute the template parameter with another constexpr functi=
on, the user doesn't have to know anything.=20
> b) this constrains the user to use a std::array=E2=80=A6 what if it were =
more appropriate for a different data structure?
> =20
It can be any literal data type, actually. The problem is that you can't al=
locate memory in a constexpr context, so your data structures are limited t=
o something whose size you can compute in advance.
Bye,
Nicola=20
--=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/.
--Apple-Mail-B0131C36-B5B1-4B02-B2EE-F3E730ABE301
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<html><head><meta http-equiv=3D"content-type" content=3D"text/html; charset=
=3Dutf-8"></head><body dir=3D"auto"><div>Il giorno 01/mag/2014, alle ore 11=
:52, <a href=3D"mailto:douglas.boffey@gmail.com">douglas.boffey@gmail.com</=
a> ha scritto:</div><div><br></div><blockquote type=3D"cite"><div><div dir=
=3D"ltr"><br>On Tuesday, 29 April 2014 13:56:07 UTC+1, Andrew Tomazos wrote=
:=20
<blockquote style=3D"BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex=
; PADDING-LEFT: 1ex" class=3D"gmail_quote">> I would like to type someth=
ing along the lines of: <br>> <br>> constexpr auto primes[5] =3D get_=
nth_prime; <br>> <br>> given a function <br>> <br>> constexpr u=
nsigned int get_nth_prime(size_t n) <br>> <br>> which would be equiva=
lent to <br>> <br>> constexpr unsigned int primes[] =3D {2, 3, 5, 7, =
11}; <br><br>Let us ignore prime sieves for the moment, and assume you have=
an <br>expensive function get_nth_prime. You can write the following=
I <br>think: <br><br> constexpr std::array<int, 5> cach=
e_primes() <br> { <br> std::array&l=
t;int, 5> A =3D {0}; <br><br> for (size_t i =
=3D 0; i < 5; i++) <br> A=
[i] =3D get_nth_primes(i); <br><br> return A; <br=
> } <br><br> constexpr std:array<int, 5> pr=
imes =3D cache_primes(); <br><br>cache_primes is called during translation =
(at compile-time), the <br>primes array is filed out and (assuming odr-used=
) is stored in the <br>object file like you want. <br><br>Is that what you =
mean? <br></blockquote>
<div> </div>
<div>This goes part way to resolving the issue, but there are still two maj=
or problems with that solution:</div>
<div> </div>
<div>a) the size of the array might not be known by the user=E2=80=94suppos=
e, for example, that I wanted to code tables for a LALR(1) parser (without =
the need for a source code generator like yacc/byacc/bison), it would be un=
reasonable for the system to expect the programmer to know the array sizes,=
and</div></div></div></blockquote><div><br></div><div>In this specific cas=
e, the size of the table is easily computed ahead of time, so you can compu=
te the template parameter with another constexpr function, the user doesn't=
have to know anything. </div><br><blockquote type=3D"cite"><div><div =
dir=3D"ltr">
<div>b) this constrains the user to use a std::array=E2=80=A6 what if it we=
re more appropriate for a different data structure?</div>
<div> </div></div>
</div></blockquote><div><br></div><div>It can be any literal data type, act=
ually. The problem is that you can't allocate memory in a constexpr context=
, so your data structures are limited to something whose size you can compu=
te in advance.</div><div><br></div><div>Bye,</div><div>Nicola </div></=
body></html>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<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 />
--Apple-Mail-B0131C36-B5B1-4B02-B2EE-F3E730ABE301--
.