Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #86376 > unrolled thread
| Started by | wij <wyniijj2@gmail.com> |
|---|---|
| First post | 2022-09-17 05:46 -0700 |
| Last post | 2022-09-17 17:32 +0200 |
| Articles | 5 on this page of 25 — 7 participants |
Back to article view | Back to comp.lang.c++
What is the good of new way of coding? wij <wyniijj2@gmail.com> - 2022-09-17 05:46 -0700
Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-17 15:03 +0200
Re: What is the good of new way of coding? Muttley@dastardlyhq.com - 2022-09-17 14:50 +0000
Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-17 17:31 +0200
Re: What is the good of new way of coding? Muttley@dastardlyhq.com - 2022-09-17 15:59 +0000
Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-17 18:03 +0200
Re: What is the good of new way of coding? Muttley@dastardlyhq.com - 2022-09-17 16:13 +0000
Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-17 18:34 +0200
Re: What is the good of new way of coding? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-09-17 19:55 +0200
Re: What is the good of new way of coding? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2022-09-17 12:03 -0700
Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-18 16:21 +0200
Re: What is the good of new way of coding? Muttley@dastardlyhq.com - 2022-09-19 08:30 +0000
Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-19 10:37 +0200
Re: What is the good of new way of coding? Muttley@dastardlyhq.com - 2022-09-19 08:54 +0000
Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-19 11:13 +0200
Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-17 15:23 +0200
Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-17 15:38 +0200
Re: What is the good of new way of coding? Andreas Kempe <kempe@lysator.liu.se> - 2022-09-17 14:37 +0000
Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-17 16:40 +0200
Re: What is the good of new way of coding? Andreas Kempe <kempe@lysator.liu.se> - 2022-09-17 20:52 +0000
Re: What is the good of new way of coding? Mr Flibble <flibble@reddwarf.jmc.corp> - 2022-09-17 22:18 +0100
Re: What is the good of new way of coding? Mr Flibble <flibble@reddwarf.jmc.corp> - 2022-09-17 22:21 +0100
Re: What is the good of new way of coding? Andreas Kempe <kempe@lysator.liu.se> - 2022-09-17 21:31 +0000
Re: What is the good of new way of coding? Muttley@dastardlyhq.com - 2022-09-17 14:52 +0000
Re: What is the good of new way of coding? Bonita Montero <Bonita.Montero@gmail.com> - 2022-09-17 17:32 +0200
Page 2 of 2 — ← Prev page 1 [2]
| From | Mr Flibble <flibble@reddwarf.jmc.corp> |
|---|---|
| Date | 2022-09-17 22:18 +0100 |
| Message-ID | <20220917221834.00005667@reddwarf.jmc.corp> |
| In reply to | #86396 |
On Sat, 17 Sep 2022 20:52:05 -0000 (UTC)
Andreas Kempe <kempe@lysator.liu.se> wrote:
> Den 2022-09-17 skrev Bonita Montero <Bonita.Montero@gmail.com>:
> > Am 17.09.2022 um 16:37 schrieb Andreas Kempe:
> >> Den 2022-09-17 skrev wij <wyniijj2@gmail.com>:
> >>> I did not update my C++ understanding (probable since C++11) to
> >>> find out I cannot understand what the codes are about !!!
> >>>
> >>> --- frorm https://groups.google.com/g/comp.lang.c++/c/2ebc6ifoPr4
> >>> #include <iostream>
> >>> #include <utility>
> >>>
> >>> using namespace std;
> >>>
> >>> int main()
> >>> {
> >>> auto pow = []<unsigned P>( double b, integral_constant<unsigned,
> >>> P> ) -> double
> >>> {
> >>> auto unroll = [&]<size_t ... Indices>( index_sequence<Indices
> >>> ...> ) -> double
> >>> {
> >>> return ((Indices, b) * ...);
> >>> };
> >>> if constexpr( P )
> >>> return unroll( make_index_sequence<P>() );
> >>> else
> >>> return b;
> >>> };
> >>> cout << pow( 2.0, integral_constant<unsigned, 10'000>() ) << endl;
> >>> }
> >>> ------
> >>>
> >>> Question1: What would the code look like if using C++2006 for the
> >>> above 'program'?
> >>
> >> As was pointed out, you can't really write that code without
> >> variadic templates. What is does at it's core, if I'm not
> >> mistaken, is expanding
> >>
> >> ((Indices, b) * ...)
> >>
> >> to
> >>
> >> (0, 2.0) * (1, 2.0) * (2, 2.0) * ... * (9999, 2.0)
> >>
> >> using modern template magic. The comma operator makes the first
> >> index value disappear and we get
> >>
> >> 2.0 * 2.0 * 2.0 * ...
> >>
> >> 10000 times. Writing something that solves the same problem with a
> >> loop could be
> >>
> >> double result = 1;
> >> for (size_t i = 0; i < 10000; i++)
> >> result *= (i, 2.0);
> >>
> >>> Question2: What is the good of such way of coding?
> >>
> >> I personally don't know. I have still to see an example of a
> >> variadic template application that I think makes sense and makes
> >> the code more readable.
> >
> > container.emplace*( ... ) is a good example.
> >
> >
>
> Ah, I didn't really consider emplace.
>
> Even though I use it myself and do see the advantage of constructing
> elements in-place in a container to avoid a move, I'm not too sure I
> really find it an improvement when it comes to readability.
> Personally, I find vector.push_back(MyType(arg1, arg2)) clearer when
> reading code. With emplace, I have to jump to the container
> declaration to find what type is being put into it.
>
> As for the implementation of emplace itself, I can of course not say
> much regarding variadic templates affecting the readability since I
> see no way of implementing the function without them.
Not all types can efficiently move (i.e. move is a copy) or they are
not even copyable ergo the rationale for emplace.
/Flibble
[toc] | [prev] | [next] | [standalone]
| From | Mr Flibble <flibble@reddwarf.jmc.corp> |
|---|---|
| Date | 2022-09-17 22:21 +0100 |
| Message-ID | <20220917222122.00004f30@reddwarf.jmc.corp> |
| In reply to | #86397 |
On Sat, 17 Sep 2022 22:18:34 +0100
Mr Flibble <flibble@reddwarf.jmc.corp> wrote:
> On Sat, 17 Sep 2022 20:52:05 -0000 (UTC)
> Andreas Kempe <kempe@lysator.liu.se> wrote:
>
> > Den 2022-09-17 skrev Bonita Montero <Bonita.Montero@gmail.com>:
> > > Am 17.09.2022 um 16:37 schrieb Andreas Kempe:
> > >> Den 2022-09-17 skrev wij <wyniijj2@gmail.com>:
> > >>> I did not update my C++ understanding (probable since C++11) to
> > >>> find out I cannot understand what the codes are about !!!
> > >>>
> > >>> --- frorm
> > >>> https://groups.google.com/g/comp.lang.c++/c/2ebc6ifoPr4
> > >>> #include <iostream> #include <utility>
> > >>>
> > >>> using namespace std;
> > >>>
> > >>> int main()
> > >>> {
> > >>> auto pow = []<unsigned P>( double b,
> > >>> integral_constant<unsigned,
> > >>> P> ) -> double
> > >>> {
> > >>> auto unroll = [&]<size_t ... Indices>( index_sequence<Indices
> > >>> ...> ) -> double
> > >>> {
> > >>> return ((Indices, b) * ...);
> > >>> };
> > >>> if constexpr( P )
> > >>> return unroll( make_index_sequence<P>() );
> > >>> else
> > >>> return b;
> > >>> };
> > >>> cout << pow( 2.0, integral_constant<unsigned, 10'000>() ) <<
> > >>> endl; }
> > >>> ------
> > >>>
> > >>> Question1: What would the code look like if using C++2006 for
> > >>> the above 'program'?
> > >>
> > >> As was pointed out, you can't really write that code without
> > >> variadic templates. What is does at it's core, if I'm not
> > >> mistaken, is expanding
> > >>
> > >> ((Indices, b) * ...)
> > >>
> > >> to
> > >>
> > >> (0, 2.0) * (1, 2.0) * (2, 2.0) * ... * (9999, 2.0)
> > >>
> > >> using modern template magic. The comma operator makes the first
> > >> index value disappear and we get
> > >>
> > >> 2.0 * 2.0 * 2.0 * ...
> > >>
> > >> 10000 times. Writing something that solves the same problem with
> > >> a loop could be
> > >>
> > >> double result = 1;
> > >> for (size_t i = 0; i < 10000; i++)
> > >> result *= (i, 2.0);
> > >>
> > >>> Question2: What is the good of such way of coding?
> > >>
> > >> I personally don't know. I have still to see an example of a
> > >> variadic template application that I think makes sense and makes
> > >> the code more readable.
> > >
> > > container.emplace*( ... ) is a good example.
> > >
> > >
> >
> > Ah, I didn't really consider emplace.
> >
> > Even though I use it myself and do see the advantage of constructing
> > elements in-place in a container to avoid a move, I'm not too sure I
> > really find it an improvement when it comes to readability.
> > Personally, I find vector.push_back(MyType(arg1, arg2)) clearer when
> > reading code. With emplace, I have to jump to the container
> > declaration to find what type is being put into it.
> >
> > As for the implementation of emplace itself, I can of course not say
> > much regarding variadic templates affecting the readability since I
> > see no way of implementing the function without them.
>
> Not all types can efficiently move (i.e. move is a copy) or they are
> not even copyable ergo the rationale for emplace.
>
> /Flibble
>
To be fair I was talking about containers in general, as far as vector
is concerned the value_type does need to be at least copyable.
/Flibble
[toc] | [prev] | [next] | [standalone]
| From | Andreas Kempe <kempe@lysator.liu.se> |
|---|---|
| Date | 2022-09-17 21:31 +0000 |
| Message-ID | <slrnticf5f.1nb3.kempe@renge.lysator.liu.se> |
| In reply to | #86398 |
Den 2022-09-17 skrev Mr Flibble <flibble@reddwarf.jmc.corp>:
> On Sat, 17 Sep 2022 22:18:34 +0100
> Mr Flibble <flibble@reddwarf.jmc.corp> wrote:
>
>> On Sat, 17 Sep 2022 20:52:05 -0000 (UTC)
>> Andreas Kempe <kempe@lysator.liu.se> wrote:
>>
>> > Den 2022-09-17 skrev Bonita Montero <Bonita.Montero@gmail.com>:
>> > > Am 17.09.2022 um 16:37 schrieb Andreas Kempe:
>> > >> Den 2022-09-17 skrev wij <wyniijj2@gmail.com>:
>> > >>> I did not update my C++ understanding (probable since C++11) to
>> > >>> find out I cannot understand what the codes are about !!!
>> > >>>
>> > >>> --- frorm
>> > >>> https://groups.google.com/g/comp.lang.c++/c/2ebc6ifoPr4
>> > >>> #include <iostream> #include <utility>
>> > >>>
>> > >>> using namespace std;
>> > >>>
>> > >>> int main()
>> > >>> {
>> > >>> auto pow = []<unsigned P>( double b,
>> > >>> integral_constant<unsigned,
>> > >>> P> ) -> double
>> > >>> {
>> > >>> auto unroll = [&]<size_t ... Indices>( index_sequence<Indices
>> > >>> ...> ) -> double
>> > >>> {
>> > >>> return ((Indices, b) * ...);
>> > >>> };
>> > >>> if constexpr( P )
>> > >>> return unroll( make_index_sequence<P>() );
>> > >>> else
>> > >>> return b;
>> > >>> };
>> > >>> cout << pow( 2.0, integral_constant<unsigned, 10'000>() ) <<
>> > >>> endl; }
>> > >>> ------
>> > >>>
>> > >>> Question1: What would the code look like if using C++2006 for
>> > >>> the above 'program'?
>> > >>
>> > >> As was pointed out, you can't really write that code without
>> > >> variadic templates. What is does at it's core, if I'm not
>> > >> mistaken, is expanding
>> > >>
>> > >> ((Indices, b) * ...)
>> > >>
>> > >> to
>> > >>
>> > >> (0, 2.0) * (1, 2.0) * (2, 2.0) * ... * (9999, 2.0)
>> > >>
>> > >> using modern template magic. The comma operator makes the first
>> > >> index value disappear and we get
>> > >>
>> > >> 2.0 * 2.0 * 2.0 * ...
>> > >>
>> > >> 10000 times. Writing something that solves the same problem with
>> > >> a loop could be
>> > >>
>> > >> double result = 1;
>> > >> for (size_t i = 0; i < 10000; i++)
>> > >> result *= (i, 2.0);
>> > >>
>> > >>> Question2: What is the good of such way of coding?
>> > >>
>> > >> I personally don't know. I have still to see an example of a
>> > >> variadic template application that I think makes sense and makes
>> > >> the code more readable.
>> > >
>> > > container.emplace*( ... ) is a good example.
>> > >
>> > >
>> >
>> > Ah, I didn't really consider emplace.
>> >
>> > Even though I use it myself and do see the advantage of constructing
>> > elements in-place in a container to avoid a move, I'm not too sure I
>> > really find it an improvement when it comes to readability.
>> > Personally, I find vector.push_back(MyType(arg1, arg2)) clearer when
>> > reading code. With emplace, I have to jump to the container
>> > declaration to find what type is being put into it.
>> >
>> > As for the implementation of emplace itself, I can of course not say
>> > much regarding variadic templates affecting the readability since I
>> > see no way of implementing the function without them.
>>
>> Not all types can efficiently move (i.e. move is a copy) or they are
>> not even copyable ergo the rationale for emplace.
>>
>> /Flibble
>>
>
> To be fair I was talking about containers in general, as far as vector
> is concerned the value_type does need to be at least copyable.
>
> /Flibble
>
Yes. I've written such types myself where I chose an std::list with
emplace to avoid having to make the type moveable or copyable. I
fully understand the need of emplace and that variadic templates are
the way to do it with the current feature set of C++. It was purely
from a readability viewpoint I dislike it.
Same thing goes for auto, really. I don't like it because I think it
lessens readability, but there are cases, like overly long iterator
types and templates/lambdas with complex or unknown return types where
it is definitely needed.
// Andreas Kempe
[toc] | [prev] | [next] | [standalone]
| From | Muttley@dastardlyhq.com |
|---|---|
| Date | 2022-09-17 14:52 +0000 |
| Message-ID | <tg4muh$82k$1@gioia.aioe.org> |
| In reply to | #86376 |
On Sat, 17 Sep 2022 05:46:57 -0700 (PDT)
wij <wyniijj2@gmail.com> wrote:
>I did not update my C++ understanding (probable since C++11) to find out I
>cannot understand what the codes are about !!!
>
>--- frorm https://groups.google.com/g/comp.lang.c++/c/2ebc6ifoPr4
>#include <iostream>
>#include <utility>
>
>using namespace std;
>
>int main()
>{
>auto pow = []<unsigned P>( double b, integral_constant<unsigned, P> )
>-> double
>{
>auto unroll = [&]<size_t ... Indices>( index_sequence<Indices ...> )
>-> double
>{
>return ((Indices, b) * ...);
>};
>if constexpr( P )
>return unroll( make_index_sequence<P>() );
>else
>return b;
>};
>cout << pow( 2.0, integral_constant<unsigned, 10'000>() ) << endl;
>}
>------
>
>Question1: What would the code look like if using C++2006 for the above
>'program'?
>Question2: What is the good of such way of coding?
Showing off. He's constantly posting convoluted code to this group desperate
for someone to say "Wow, impressive, you're so clever!"
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2022-09-17 17:32 +0200 |
| Message-ID | <tg4p90$6kku$2@dont-email.me> |
| In reply to | #86386 |
Am 17.09.2022 um 16:52 schrieb Muttley@dastardlyhq.com:
> On Sat, 17 Sep 2022 05:46:57 -0700 (PDT)
> wij <wyniijj2@gmail.com> wrote:
>> I did not update my C++ understanding (probable since C++11) to find out I
>> cannot understand what the codes are about !!!
>>
>> --- frorm https://groups.google.com/g/comp.lang.c++/c/2ebc6ifoPr4
>> #include <iostream>
>> #include <utility>
>>
>> using namespace std;
>>
>> int main()
>> {
>> auto pow = []<unsigned P>( double b, integral_constant<unsigned, P> )
>> -> double
>> {
>> auto unroll = [&]<size_t ... Indices>( index_sequence<Indices ...> )
>> -> double
>> {
>> return ((Indices, b) * ...);
>> };
>> if constexpr( P )
>> return unroll( make_index_sequence<P>() );
>> else
>> return b;
>> };
>> cout << pow( 2.0, integral_constant<unsigned, 10'000>() ) << endl;
>> }
>> ------
>>
>> Question1: What would the code look like if using C++2006 for the above
>> 'program'?
>> Question2: What is the good of such way of coding?
>
> Showing off. He's constantly posting convoluted code to this group desperate
> for someone to say "Wow, impressive, you're so clever!"
>
I don't wanted to show sth. impressive but I wanted to make
others to compile that.
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.c++
csiph-web