Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #70664 > unrolled thread

Two silly problems

Started by"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
First post2019-12-26 16:47 +0100
Last post2019-12-28 17:15 +0000
Articles 11 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  Two silly problems "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-12-26 16:47 +0100
    Re: Two silly problems "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-12-26 16:51 +0100
    Re: Two silly problems Bo Persson <bo@bo-persson.se> - 2019-12-26 16:55 +0100
      Re: Two silly problems "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-12-27 05:07 +0100
        Re: Two silly problems Mr Flibble <flibble@i42.removethisbit.co.uk> - 2019-12-27 16:38 +0000
          Re: Two silly problems "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-12-27 20:05 +0100
            Re: Two silly problems Mr Flibble <flibble@i42.removethisbit.co.uk> - 2019-12-28 00:42 +0000
              Re: Two silly problems "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-12-28 07:37 +0100
                Re: Two silly problems Mr Flibble <flibble@i42.removethisbit.co.uk> - 2019-12-28 15:51 +0000
                  Re: Two silly problems boltar@nowhere.org - 2019-12-28 16:30 +0000
                    Re: Two silly problems Mr Flibble <flibble@i42.removethisbit.co.uk> - 2019-12-28 17:15 +0000

#70664 — Two silly problems

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2019-12-26 16:47 +0100
SubjectTwo silly problems
Message-ID<qu2kmo$6f4$1@dont-email.me>
Why, with the below code, do I get this result:

 > echo no no no not ever | a
ever 1
no 1
not 1

I had expected a count of three for "no".

Also, how the feck does the commented line cause a Most Vexing Parse? I 
was sure that by having a constructor call with `cin` as argument there, 
i.e. a value argument, there could be no vexing parse. But g++ just 
doesn't like it and then misleadingly complains about something later in 
the code, using an avalanche of idiot diagnostics so as to ensure one 
would waste time on it.


-------------------------------------------------
// Read a file of text, determine the n most frequently used words, and 
print out a sorted
// list of those words along with their frequencies.
//
// 
https://medium.com/@vgasparyan1995/a-little-bit-of-code-c-20-ranges-c6a6f7eae401

// Assuming
// * ASCII lowercase text
// * No punctuation.

#include <iterator>
#include <iostream>
#include <string>
#include <unordered_set>

template< class Key > using Multiset_ = std::unordered_set<Key>;

auto main()
     -> int
{
     using std::cin, std::cout, std::endl, std::string;
     using Iit = std::istream_iterator<string>;
     const Iit end = Iit();
     Multiset_<string> words( Iit( cin ), (Iit()) );       // OK
     //Multiset_<string> words( Iit( cin ), Iit() );       //! Most 
vexing parse?
     for( const string& s: words ) {
         cout << s << ' ' << words.count( s ) << endl;
     }
}
-------------------------------------------------


- Alf (possibly in superstupido-mode, for various reasons)

[toc] | [next] | [standalone]


#70665

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2019-12-26 16:51 +0100
Message-ID<qu2kuk$7p5$1@dont-email.me>
In reply to#70664
On 26.12.2019 16:47, Alf P. Steinbach wrote:
> Why, with the below code, do I get this result:
> 
>  > echo no no no not ever | a
> ever 1
> no 1
> not 1
> 
> I had expected a count of three for "no".

Oh, just a typo. I had forgot to writ "multi" in std::unordered_multiset.


> Also, how the feck does the commented line cause a Most Vexing Parse? I 
> was sure that by having a constructor call with `cin` as argument there, 
> i.e. a value argument, there could be no vexing parse. But g++ just 
> doesn't like it and then misleadingly complains about something later in 
> the code, using an avalanche of idiot diagnostics so as to ensure one 
> would waste time on it.

But this? I can't get my head around it. Right now.


> -------------------------------------------------
> // Read a file of text, determine the n most frequently used words, and 
> print out a sorted
> // list of those words along with their frequencies.
> //
> // 
> https://medium.com/@vgasparyan1995/a-little-bit-of-code-c-20-ranges-c6a6f7eae401 
> 
> 
> // Assuming
> // * ASCII lowercase text
> // * No punctuation.
> 
> #include <iterator>
> #include <iostream>
> #include <string>
> #include <unordered_set>
> 
> template< class Key > using Multiset_ = std::unordered_set<Key>;
> 
> auto main()
>      -> int
> {
>      using std::cin, std::cout, std::endl, std::string;
>      using Iit = std::istream_iterator<string>;
>      const Iit end = Iit();
>      Multiset_<string> words( Iit( cin ), (Iit()) );       // OK
>      //Multiset_<string> words( Iit( cin ), Iit() );       //! Most 
> vexing parse?
>      for( const string& s: words ) {
>          cout << s << ' ' << words.count( s ) << endl;
>      }
> }
> -------------------------------------------------
> 
> 
> - Alf (possibly in superstupido-mode, for various reasons)

[toc] | [prev] | [next] | [standalone]


#70666

FromBo Persson <bo@bo-persson.se>
Date2019-12-26 16:55 +0100
Message-ID<h6k3g7Fp6iaU1@mid.individual.net>
In reply to#70664
On 2019-12-26 at 16:47, Alf P. Steinbach wrote:
> Why, with the below code, do I get this result:
> 
>  > echo no no no not ever | a
> ever 1
> no 1
> not 1
> 
> I had expected a count of three for "no".
> 
> Also, how the feck does the commented line cause a Most Vexing Parse? I 
> was sure that by having a constructor call with `cin` as argument there, 
> i.e. a value argument, there could be no vexing parse. But g++ just 
> doesn't like it and then misleadingly complains about something later in 
> the code, using an avalanche of idiot diagnostics so as to ensure one 
> would waste time on it.
> 
> 
> -------------------------------------------------
> // Read a file of text, determine the n most frequently used words, and 
> print out a sorted
> // list of those words along with their frequencies.
> //
> // 
> https://medium.com/@vgasparyan1995/a-little-bit-of-code-c-20-ranges-c6a6f7eae401 
> 
> 
> // Assuming
> // * ASCII lowercase text
> // * No punctuation.
> 
> #include <iterator>
> #include <iostream>
> #include <string>
> #include <unordered_set>
> 
> template< class Key > using Multiset_ = std::unordered_set<Key>;
> 
> auto main()
>      -> int
> {
>      using std::cin, std::cout, std::endl, std::string;
>      using Iit = std::istream_iterator<string>;
>      const Iit end = Iit();
>      Multiset_<string> words( Iit( cin ), (Iit()) );       // OK
>      //Multiset_<string> words( Iit( cin ), Iit() );       //! Most 
> vexing parse?
>      for( const string& s: words ) {
>          cout << s << ' ' << words.count( s ) << endl;
>      }
> }
> -------------------------------------------------
> 
> 
> - Alf (possibly in superstupido-mode, for various reasons)

Some more coffee needed, possibly?

Multiset_ sounds like a multiset, but it really is not if we read the 
using-line carefully.  :-)

And for the most vexing parse - cin is a value, but it *could* also be 
the name of a parameter for a (hypothetical) words function. So parsing 
a declaration wins!


    Bo Persson


[toc] | [prev] | [next] | [standalone]


#70673

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2019-12-27 05:07 +0100
Message-ID<qu401n$vh4$1@dont-email.me>
In reply to#70666
On 26.12.2019 16:55, Bo Persson wrote:
> On 2019-12-26 at 16:47, Alf P. Steinbach wrote:
>> Why, with the below code, do I get this result:
>>
>>  > echo no no no not ever | a
>> ever 1
>> no 1
>> not 1
>>
>> I had expected a count of three for "no".
>>
>> Also, how the feck does the commented line cause a Most Vexing Parse? 
>> I was sure that by having a constructor call with `cin` as argument 
>> there, i.e. a value argument, there could be no vexing parse. But g++ 
>> just doesn't like it and then misleadingly complains about something 
>> later in the code, using an avalanche of idiot diagnostics so as to 
>> ensure one would waste time on it.
>>
>>
>> -------------------------------------------------
>> // Read a file of text, determine the n most frequently used words, 
>> and print out a sorted
>> // list of those words along with their frequencies.
>> //
>> // 
>> https://medium.com/@vgasparyan1995/a-little-bit-of-code-c-20-ranges-c6a6f7eae401 
>>
>>
>> // Assuming
>> // * ASCII lowercase text
>> // * No punctuation.
>>
>> #include <iterator>
>> #include <iostream>
>> #include <string>
>> #include <unordered_set>
>>
>> template< class Key > using Multiset_ = std::unordered_set<Key>;
>>
>> auto main()
>>      -> int
>> {
>>      using std::cin, std::cout, std::endl, std::string;
>>      using Iit = std::istream_iterator<string>;
>>      const Iit end = Iit();
>>      Multiset_<string> words( Iit( cin ), (Iit()) );       // OK
>>      //Multiset_<string> words( Iit( cin ), Iit() );       //! Most 
>> vexing parse?
>>      for( const string& s: words ) {
>>          cout << s << ' ' << words.count( s ) << endl;
>>      }
>> }
>> -------------------------------------------------
>>
>>
>> - Alf (possibly in superstupido-mode, for various reasons)
> 
> Some more coffee needed, possibly?
> 
> Multiset_ sounds like a multiset, but it really is not if we read the 
> using-line carefully.  :-)
> 
> And for the most vexing parse - cin is a value, but it *could* also be 
> the name of a parameter for a (hypothetical) words function. So parsing 
> a declaration wins!

Thanks.

I wanted to see how conventional C++17 code fares against the C++20 
Range-based pipeline stuff in the linked to article

https://medium.com/@vgasparyan1995/a-little-bit-of-code-c-20-ranges-c6a6f7eae401

So the code below is what I came up with. It relies on two of my own 
libraries for some small stuff that avoids a details mess.

To me this conventional code is easier to read and to reason about than 
the Ranges code, but to the author of that article it's evidently 
opposite. I suspect that most of those who love Ranges are of a 
mathematical inclination, and that it reminds them of functional 
programming. Maybe.


--------------------------------------------------------

// Read a file of text, determine the n most frequently used words, and 
print out a sorted
// list of those words along with their frequencies.
//
// 
https://medium.com/@vgasparyan1995/a-little-bit-of-code-c-20-ranges-c6a6f7eae401

// Assuming
// * ASCII text.

#include <cppx-core-language/all.hpp>   // 
https://github.com/alf-p-steinbach/cppx-core-language
#include <cpp/all.hpp>                  // 
https://github.com/alf-p-steinbach/Cpp-Header-Collections

namespace app {
     $use_std( cin, cout, endl, quoted, string, string_view, vector, min );
     template< class Key, class Value > using Map_ = 
std::unordered_map<Key, Value>;

     $use_cppx( Truth, Size, Index, hopefully, fail, span_of, string_to_ );
     namespace ascii = cppx::ascii;
     using namespace cppx::syntax;               // String assembly via 
"<<".

     auto sans_punctuation( const string_view& s )
         -> string
     {
         string result;
         for( const char ch: s ) {
             if( not ascii::is_punctuation( ch ) ) {
                 result += ch;
             }
         }
         return result;
     }

     auto words_and_counts()
         -> auto
     {
         Map_<string, Size> result;
         using In_it = std::istream_iterator<string>;
         for( const string& s: span_of( In_it( cin ), In_it() ) ) {
             const string word = ascii::to_lowercase( sans_punctuation( 
s ) );
             if( word.length() != 0 ) {
                 ++result[word];
             }
         }
         return result;
     }

     struct Word_info { string text; Size count; };

     auto order_by_decreasing_counts( const Word_info& a, const 
Word_info& b )
         -> Truth
     { return a.count > b.count; }

     void run( const vector<string_view>& command_parts )
     {
         hopefully( command_parts.size() == 2 )
             or fail( "Usage: "s << command_parts[0] << " N_ITEMS" );

         const Size n = string_to_<Size>( command_parts[1] );
         vector<Word_info> sorted;
         for( const auto& pair: words_and_counts() ) {
             sorted.push_back( {pair.first, pair.second} );
         }
         sort( $items_of( sorted ), order_by_decreasing_counts );
         $repeat_times( min<Size>( n, sorted.size() ) ) {
             cout    << _i + 1 << ": "
                     << quoted( sorted[_i].text ) << ' ' << 
sorted[_i].count << " time(s)"
                     << endl;
         }
     }
}  // namespace app

auto main( int n_args, char** args )
     -> int
{
     $use_std( vector, string_view, exception, cerr, endl );
     try {
         app::run( vector<string_view>( args, args + n_args ) );
         return EXIT_SUCCESS;
     } catch( const exception& x ) {
         cerr << "!" << x.what() << endl;
     }
     return EXIT_FAILURE;
}
--------------------------------------------------------

- Alf

[toc] | [prev] | [next] | [standalone]


#70688

FromMr Flibble <flibble@i42.removethisbit.co.uk>
Date2019-12-27 16:38 +0000
Message-ID<4sqNF.162115$eP4.139233@fx23.dcg>
In reply to#70673
On 27/12/2019 04:07, Alf P. Steinbach wrote:

>      $use_std( cin, cout, endl, quoted, string, string_view, vector, min );

Egregious (std:: preferred).

>      template< class Key, class Value > using Map_ = 
> std::unordered_map<Key, Value>;

Egregious (adds nothing but obfuscation).

>      $use_cppx( Truth, Size, Index, hopefully, fail, span_of, string_to_ );

Egregious (std:: preferred).

>          hopefully( command_parts.size() == 2 )
>              or fail( "Usage: "s << command_parts[0] << " N_ITEMS" );

Egregious (adds nothing but obfuscation).

>          $repeat_times( min<Size>( n, sorted.size() ) ) {

Egregious (adds nothing but obfuscation).

> auto main( int n_args, char** args )
>      -> int

Egregious (delibarate troll or OCD).

>      $use_std( vector, string_view, exception, cerr, endl );

Egregious (std:: preferred).

> - Alf

/Flibble

-- 
“You won’t burn in hell. But be nice anyway.” – Ricky Gervais
“I see Atheists are fighting and killing each other again, over who 
doesn’t believe in any God the most. Oh, no..wait.. that never happens.” 
– Ricky Gervais
"Suppose it's all true, and you walk up to the pearly gates, and are 
confronted by God," Bryne asked on his show The Meaning of Life. "What 
will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery 
that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates 
a world that is so full of injustice and pain. That's what I would say."

[toc] | [prev] | [next] | [standalone]


#70694

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2019-12-27 20:05 +0100
Message-ID<qu5kmb$qvh$1@dont-email.me>
In reply to#70688
On 27.12.2019 17:38, Mr Flibble wrote:
> 
> Egregious (std:: preferred).

Why do you prefer to repeat a bit of text ad nauseam? It's more to 
write, more to read, recommended against by a number of generally 
accepted guidelines like DRY, and just plain freaking dumb. So, why?

- Alf

[toc] | [prev] | [next] | [standalone]


#70711

FromMr Flibble <flibble@i42.removethisbit.co.uk>
Date2019-12-28 00:42 +0000
Message-ID<gyxNF.89127$3sa.52754@fx02.dcg>
In reply to#70694
On 27/12/2019 19:05, Alf P. Steinbach wrote:
> On 27.12.2019 17:38, Mr Flibble wrote:
>>
>> Egregious (std:: preferred).
> 
> Why do you prefer to repeat a bit of text ad nauseam? It's more to 
> write, more to read, recommended against by a number of generally 
> accepted guidelines like DRY, and just plain freaking dumb. So, why?

Taking your really bad analogy of DRY to its logical conclusion you can 
only use a vector once in your entire program so you don't fucking 
repeat yourself.  Bad analogies are "just plain freaking dumb".

/Flibble

-- 
“You won’t burn in hell. But be nice anyway.” – Ricky Gervais
“I see Atheists are fighting and killing each other again, over who 
doesn’t believe in any God the most. Oh, no..wait.. that never happens.” 
– Ricky Gervais
"Suppose it's all true, and you walk up to the pearly gates, and are 
confronted by God," Bryne asked on his show The Meaning of Life. "What 
will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery 
that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates 
a world that is so full of injustice and pain. That's what I would say."

[toc] | [prev] | [next] | [standalone]


#70717

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2019-12-28 07:37 +0100
Message-ID<qu6t75$tus$1@dont-email.me>
In reply to#70711
On 28.12.2019 01:42, Mr Flibble wrote:
> On 27/12/2019 19:05, Alf P. Steinbach wrote:
>> On 27.12.2019 17:38, Mr Flibble wrote:
>>>
>>> Egregious (std:: preferred).
>>
>> Why do you prefer to repeat a bit of text ad nauseam? It's more to 
>> write, more to read, recommended against by a number of generally 
>> accepted guidelines like DRY, and just plain freaking dumb. So, why?
> 
> Taking your really bad analogy of DRY to its logical conclusion you can 
> only use a vector once in your entire program so you don't fucking 
> repeat yourself.  Bad analogies are "just plain freaking dumb".

I can agree with the last sentence.

Regarding the rest, consider:


#include <array>

void leigh()
{
     const int board_size = 8;
     std::array<std::array<int, board_size>, board_size> board =
     {
         std::array<int, board_size>{}, std::array<int, board_size>{},
         std::array<int, board_size>{}, std::array<int, board_size>{},
         std::array<int, board_size>{}, std::array<int, board_size>{},
         std::array<int, board_size>{}, std::array<int, board_size>{}
     };
     (void) board; // Whatever
}

void alf()
{
     using std::array;

     const int width     = 8;
     const int height    = width;

     using Row       = array<int, width>;
     using Board     = array<Row, height>;

     Board board{};
     (void) board; // Whatever
}


Here the two functions manage to create a local variable called `board`, 
of the exact same type in the code as by your argumentation you would 
have written it, and in corresponding code by me.

In the assumed-like-Leigh code the `std::` qualification and the type 
builder `array` is repeated ad nauseam, because you argue that even when 
it's known that all textual instances are a given text it should better 
be spelled out; and the focus, what's communicated to a reader, is on 
the details of the data representation and not on what it models.

In the more DRY (Don't Repeat Yourself) Alf code the relevant namespace 
`std::` is mentioned only once; the type builder `array` is mentioned 
only in a `using` declaration and for the two types that it builds here, 
that is, three times; and the focus, what's communicated to a reader, is 
on what the structure models.

- Alf

[toc] | [prev] | [next] | [standalone]


#70735

FromMr Flibble <flibble@i42.removethisbit.co.uk>
Date2019-12-28 15:51 +0000
Message-ID<5SKNF.20703$vj3.18073@fx10.dcg>
In reply to#70717
On 28/12/2019 06:37, Alf P. Steinbach wrote:
> On 28.12.2019 01:42, Mr Flibble wrote:
>> On 27/12/2019 19:05, Alf P. Steinbach wrote:
>>> On 27.12.2019 17:38, Mr Flibble wrote:
>>>>
>>>> Egregious (std:: preferred).
>>>
>>> Why do you prefer to repeat a bit of text ad nauseam? It's more to 
>>> write, more to read, recommended against by a number of generally 
>>> accepted guidelines like DRY, and just plain freaking dumb. So, why?
>>
>> Taking your really bad analogy of DRY to its logical conclusion you 
>> can only use a vector once in your entire program so you don't fucking 
>> repeat yourself.  Bad analogies are "just plain freaking dumb".
> 
> I can agree with the last sentence.
> 
> Regarding the rest, consider:
> 
> 
> #include <array>
> 
> void leigh()
> {
>      const int board_size = 8;
>      std::array<std::array<int, board_size>, board_size> board =
>      {
>          std::array<int, board_size>{}, std::array<int, board_size>{},
>          std::array<int, board_size>{}, std::array<int, board_size>{},
>          std::array<int, board_size>{}, std::array<int, board_size>{},
>          std::array<int, board_size>{}, std::array<int, board_size>{}
>      };
>      (void) board; // Whatever
> }
> 
> void alf()
> {
>      using std::array;
> 
>      const int width     = 8;
>      const int height    = width;
> 
>      using Row       = array<int, width>;
>      using Board     = array<Row, height>;
> 
>      Board board{};
>      (void) board; // Whatever
> }
> 
> 
> Here the two functions manage to create a local variable called `board`, 
> of the exact same type in the code as by your argumentation you would 
> have written it, and in corresponding code by me.
> 
> In the assumed-like-Leigh code the `std::` qualification and the type 
> builder `array` is repeated ad nauseam, because you argue that even when 
> it's known that all textual instances are a given text it should better 
> be spelled out; and the focus, what's communicated to a reader, is on 
> the details of the data representation and not on what it models.
> 
> In the more DRY (Don't Repeat Yourself) Alf code the relevant namespace 
> `std::` is mentioned only once; the type builder `array` is mentioned 
> only in a `using` declaration and for the two types that it builds here, 
> that is, three times; and the focus, what's communicated to a reader, is 
> on what the structure models.

Alf, using deliberate exaggeration to try to invalidate a person's 
argument is disingenuous at best but I am sure you know this.

void real_leigh()
{
     std::size_t const width     = 8;
     std::size_t const height    = width;

     using row       = std::array<int, width>;
     using board     = std::array<row, height>;

     Board board{};
     (void) board; // Whatever
}

Now please stop being a cunt.

/Flibble

-- 
“You won’t burn in hell. But be nice anyway.” – Ricky Gervais
“I see Atheists are fighting and killing each other again, over who 
doesn’t believe in any God the most. Oh, no..wait.. that never happens.” 
– Ricky Gervais
"Suppose it's all true, and you walk up to the pearly gates, and are 
confronted by God," Bryne asked on his show The Meaning of Life. "What 
will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery 
that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates 
a world that is so full of injustice and pain. That's what I would say."

[toc] | [prev] | [next] | [standalone]


#70737

Fromboltar@nowhere.org
Date2019-12-28 16:30 +0000
Message-ID<qu7vvs$129g$1@gioia.aioe.org>
In reply to#70735
On Sat, 28 Dec 2019 15:51:29 +0000
Mr Flibble <flibble@i42.removethisbit.co.uk> wrote:
>On 28/12/2019 06:37, Alf P. Steinbach wrote:
>> Here the two functions manage to create a local variable called `board`, 
>> of the exact same type in the code as by your argumentation you would 
>> have written it, and in corresponding code by me.
>> 
>> In the assumed-like-Leigh code the `std::` qualification and the type 
>> builder `array` is repeated ad nauseam, because you argue that even when 
>> it's known that all textual instances are a given text it should better 
>> be spelled out; and the focus, what's communicated to a reader, is on 
>> the details of the data representation and not on what it models.
>> 
>> In the more DRY (Don't Repeat Yourself) Alf code the relevant namespace 
>> `std::` is mentioned only once; the type builder `array` is mentioned 
>> only in a `using` declaration and for the two types that it builds here, 
>> that is, three times; and the focus, what's communicated to a reader, is 
>> on what the structure models.
>
>Alf, using deliberate exaggeration to try to invalidate a person's 
>argument is disingenuous at best but I am sure you know this.
>
>void real_leigh()
>{
>     std::size_t const width     = 8;
>     std::size_t const height    = width;
>
>     using row       = std::array<int, width>;
>     using board     = std::array<row, height>;
>
>     Board board{};
>     (void) board; // Whatever
>}

Or since board is just storing ints you could both make it even simpler still 
using C99:

	size_t const width     = 8;
	size_t const height    = width;

	typedef int Board[width][height];
	Board board;

Just saying.

[toc] | [prev] | [next] | [standalone]


#70741

FromMr Flibble <flibble@i42.removethisbit.co.uk>
Date2019-12-28 17:15 +0000
Message-ID<v4MNF.45404$YR4.27837@fx18.dcg>
In reply to#70737
On 28/12/2019 16:30, boltar@nowhere.org wrote:
> On Sat, 28 Dec 2019 15:51:29 +0000
> Mr Flibble <flibble@i42.removethisbit.co.uk> wrote:
>> On 28/12/2019 06:37, Alf P. Steinbach wrote:
>>> Here the two functions manage to create a local variable called `board`,
>>> of the exact same type in the code as by your argumentation you would
>>> have written it, and in corresponding code by me.
>>>
>>> In the assumed-like-Leigh code the `std::` qualification and the type
>>> builder `array` is repeated ad nauseam, because you argue that even when
>>> it's known that all textual instances are a given text it should better
>>> be spelled out; and the focus, what's communicated to a reader, is on
>>> the details of the data representation and not on what it models.
>>>
>>> In the more DRY (Don't Repeat Yourself) Alf code the relevant namespace
>>> `std::` is mentioned only once; the type builder `array` is mentioned
>>> only in a `using` declaration and for the two types that it builds here,
>>> that is, three times; and the focus, what's communicated to a reader, is
>>> on what the structure models.
>>
>> Alf, using deliberate exaggeration to try to invalidate a person's
>> argument is disingenuous at best but I am sure you know this.
>>
>> void real_leigh()
>> {
>>      std::size_t const width     = 8;
>>      std::size_t const height    = width;
>>
>>      using row       = std::array<int, width>;
>>      using board     = std::array<row, height>;
>>
>>      Board board{};
>>      (void) board; // Whatever
>> }
> 
> Or since board is just storing ints you could both make it even simpler still
> using C99:
> 
> 	size_t const width     = 8;
> 	size_t const height    = width;
> 
> 	typedef int Board[width][height];
> 	Board board;
> 
> Just saying.

Simpler isn't always better and one could argue that old C-style arrays 
can make certain code that *uses* them more complicated versus 
std::array usage.  More or less the only use-case for C-style arrays in 
modern C++ are dynamic arrays for allocating uninitialized buffers; 
std::array is superior to C-style arrays for nearly all other use-cases 
due to the container-like interface it offers with NO associated 
overhead in offering it. Just saying.

/Flibble

-- 
“You won’t burn in hell. But be nice anyway.” – Ricky Gervais
“I see Atheists are fighting and killing each other again, over who 
doesn’t believe in any God the most. Oh, no..wait.. that never happens.” 
– Ricky Gervais
"Suppose it's all true, and you walk up to the pearly gates, and are 
confronted by God," Bryne asked on his show The Meaning of Life. "What 
will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery 
that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates 
a world that is so full of injustice and pain. That's what I would say."

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web