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


Groups > comp.lang.c++ > #72243

Structured binding considered harmful

From jacobnavia <jacob@jacob.remcomp.fr>
Newsgroups comp.lang.c++
Subject Structured binding considered harmful
Date 2020-03-13 00:13 +0100
Organization A noiseless patient Spider
Message-ID <r4efnn$f41$1@dont-email.me> (permalink)

Show all headers | View raw


Here is one exemple of "structured binding" from the paper of Filipek:

int main() {
// Initialize a map table.
const std::map<std::string, int> mapCityPopulation {
{ "Beijing", 21'707'000 },
{ "London", 8'787'892 },
{ "New York", 8'622'698 }
};

for (auto&[city, population] : mapCityPopulation)
      std::cout << city << ": " << population << '\n';
}

Within the for statement the variables "city" and "population" will be 
bound automatically to the corresponding fields of the map.

Cute isn't it?

Before, in the old times, you would have to write:
(I cite the book of Filipek page 29)

<quote>

Consider a function that returns two results in a pair:

std::pair<int, bool> InsertElement(int el) { ... }

You can write:
auto ret = InsertElement(...)
And then refer to ret.first or ret.second. However, referring to values 
as .first or .second is also not expressive - you can easily confuse the 
names, and it’s hard to read.
<end quote>

Yes, maybe is not "expressive". There is an easy work around though, as 
Filipek shows:

<quote>
Alternatively you can leverage std::tie which will unpack the tuple/pair 
into local variables:
int index { 0 };
bool flag { false };
std::tie(index, flag) = InsertElement(10);
Such code might be useful when you work with std::set::insert which 
returns std::pair:std::set<int> mySet;

std::set<int>::iterator iter;
bool inserted { false };

std::tie(iter, inserted) = mySet.insert(10);
if (inserted)
      std::cout << "Value was inserted\n";

As you see, such a simple pattern - returning several values from a 
function - requires several lines of code.
<end quote>

Yes, several lines of code that is easily read by anyone and by existing 
tools.

And here awe arrive at the essence of why C++ is going into endless 
complexity: a desire to have "cute" constructs and save some seconds to 
the programmer typing the new code in.

Nothing will be said about the need to reprogram all tools that read C++ 
code to accept the new stuff, the need to figure out all ramifications 
of the new construct, etc etc.

Because the consequences are terrible when you do not have a 1 to 1 
correspondence between the names in the bracket enclosed list, and the 
class whose fields will be "bound" to those names.


Even WORST:

What happens when the class is modified to have a new field?
All the structured binding constructs have to be modified to add the new 
field, and if you miss one crashes and bugs will inevitably happen.

All of this is not necessary using the old syntax that was more verbose 
but MUCH SAFER and robust.

MINDLESS COMPLXITY FOR COMPLEXITY's SAKE, with no improvement to 
anything useful.

All the tools have to be rewritten to accomodate this nonsense, what is 
not easy since you have to get the definition of the class object that 
can be anywhere else, in another header file or whatever!

What is the end result?

A new useless construct has been added to the language that serves no 
purpose other than saving some seconds when typing C++.

GREAT!

Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Structured binding considered harmful jacobnavia <jacob@jacob.remcomp.fr> - 2020-03-13 00:13 +0100
  Re: Structured binding considered harmful Maciej Sobczak <see.my.homepage@gmail.com> - 2020-03-13 00:46 -0700
    Re: Structured binding considered harmful jacobnavia <jacob@jacob.remcomp.fr> - 2020-03-14 09:10 +0100
  Re: Structured binding considered harmful Öö Tiib <ootiib@hot.ee> - 2020-03-13 09:40 -0700
    Re: Structured binding considered harmful jacobnavia <jacob@jacob.remcomp.fr> - 2020-03-14 09:05 +0100
      Re: Structured binding considered harmful Öö Tiib <ootiib@hot.ee> - 2020-03-14 03:19 -0700
        Re: Structured binding considered harmful Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-03-15 11:08 +0000
          Re: Structured binding considered harmful Öö Tiib <ootiib@hot.ee> - 2020-03-15 09:46 -0700
            Re: Structured binding considered harmful "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2020-03-15 19:27 +0100
              Re: Structured binding considered harmful Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-15 15:15 -0700
                Re: Structured binding considered harmful "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2020-03-16 05:23 +0100
                Re: Structured binding considered harmful "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2020-03-16 05:54 +0100
              Re: Structured binding considered harmful Öö Tiib <ootiib@hot.ee> - 2020-03-16 00:20 -0700
  Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-13 23:08 -0400
    Re: Structured binding considered harmful jacobnavia <jacob@jacob.remcomp.fr> - 2020-03-14 08:59 +0100
      Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-14 10:09 -0400
        Re: Structured binding considered harmful jacobnavia <jacob@jacob.remcomp.fr> - 2020-03-14 18:32 +0100
          Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-14 18:33 -0400
  Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-15 17:50 +0100
    Re: Structured binding considered harmful jacobnavia <jacob@jacob.remcomp.fr> - 2020-03-16 00:11 +0100
      Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-16 05:24 +0100
        Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-16 06:46 -0400
          Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-16 12:52 +0100
            Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-16 08:32 -0400
              Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-16 13:51 +0100
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-16 20:06 -0400
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-17 06:37 +0100
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-17 07:17 -0400
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-17 18:37 +0100
                Re: Structured binding considered harmful "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2020-03-17 18:50 +0100
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-18 06:30 -0400
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-18 11:38 +0100
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-18 08:17 -0400
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-18 13:19 +0100
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-18 08:31 -0400
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-18 14:15 +0100
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-18 10:13 -0400
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-18 15:45 +0100
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-18 11:36 -0400
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-18 17:59 +0100
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-18 13:18 -0400
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-18 18:27 +0100
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-18 14:41 -0400
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-18 19:46 +0100
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-18 19:46 -0400
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-19 06:41 +0100
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-19 07:02 -0400
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-19 12:41 +0100
                Re: Structured binding considered harmful Öö Tiib <ootiib@hot.ee> - 2020-03-19 06:10 -0700
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-19 14:15 +0100
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-19 19:23 -0400
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-20 09:18 +0100
                Re: Structured binding considered harmful James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-20 05:31 -0700
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-20 13:40 +0100
                Re: Structured binding considered harmful Öö Tiib <ootiib@hot.ee> - 2020-03-20 08:50 -0700
                Re: Structured binding considered harmful Daniel <danielaparker@gmail.com> - 2020-03-20 09:30 -0700
                Re: Structured binding considered harmful Öö Tiib <ootiib@hot.ee> - 2020-03-20 10:13 -0700
                Re: Structured binding considered harmful Daniel <danielaparker@gmail.com> - 2020-03-20 10:36 -0700
                Re: Structured binding considered harmful Ian Collins <ian-news@hotmail.com> - 2020-03-21 12:06 +1300
                Re: Structured binding considered harmful Öö Tiib <ootiib@hot.ee> - 2020-03-20 16:37 -0700
                Re: Structured binding considered harmful Daniel <danielaparker@gmail.com> - 2020-03-20 17:17 -0700
                Re: Structured binding considered harmful Öö Tiib <ootiib@hot.ee> - 2020-03-21 10:09 -0700
                Re: Structured binding considered harmful Daniel <danielaparker@gmail.com> - 2020-03-21 11:21 -0700
                Re: Structured binding considered harmful Öö Tiib <ootiib@hot.ee> - 2020-03-21 13:09 -0700
                Re: Structured binding considered harmful Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-03-21 22:33 +0000
                Re: Structured binding considered harmful Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-03-20 17:30 +0000
                Re: Structured binding considered harmful Daniel <danielaparker@gmail.com> - 2020-03-20 10:49 -0700
                Re: Structured binding considered harmful Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-03-20 18:47 +0000
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-20 17:41 +0100
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-20 18:06 -0400
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-21 06:31 +0100
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-21 09:50 -0400
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-21 14:59 +0100
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-21 10:42 -0400
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-21 16:09 +0100
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-21 11:12 -0400
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-20 18:05 -0400
                Re: Structured binding considered harmful Daniel <danielaparker@gmail.com> - 2020-03-20 15:57 -0700
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-20 23:23 -0400
                Re: Structured binding considered harmful Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-03-20 21:08 -0700
                Re: Structured binding considered harmful Ralf Goertz <me@myprovider.invalid> - 2020-03-21 08:24 +0100
                Re: Structured binding considered harmful Daniel <danielaparker@gmail.com> - 2020-03-21 01:36 -0700
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-21 06:30 +0100
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-21 09:51 -0400
                Re: Structured binding considered harmful jacobnavia <jacob@jacob.remcomp.fr> - 2020-03-22 13:22 +0100
                Re: Structured binding considered harmful Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-03-22 13:24 +0000
                Re: Structured binding considered harmful James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-22 06:32 -0700
                Re: Structured binding considered harmful Daniel <danielaparker@gmail.com> - 2020-03-22 07:59 -0700
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-22 13:02 -0400
                Re: Structured binding considered harmful Melzzzzz <Melzzzzz@zzzzz.com> - 2020-03-22 17:09 +0000
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-22 13:35 -0400
                Re: Structured binding considered harmful Melzzzzz <Melzzzzz@zzzzz.com> - 2020-03-22 17:54 +0000
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-22 17:19 -0400
                Re: Structured binding considered harmful Melzzzzz <Melzzzzz@zzzzz.com> - 2020-03-22 21:46 +0000
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-22 19:06 -0400
                Re: Structured binding considered harmful danielaparker@gmail.com - 2020-03-22 17:11 -0700
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-22 22:16 -0400
                Re: Structured binding considered harmful Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2020-03-25 20:38 +0000
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-25 20:01 -0400
                Re: Structured binding considered harmful danielaparker@gmail.com - 2020-03-22 10:58 -0700
                Re: Structured binding considered harmful Ian Collins <ian-news@hotmail.com> - 2020-03-23 07:41 +1300
                Re: Structured binding considered harmful James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-22 15:01 -0400
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-22 10:24 -0400
                Re: Structured binding considered harmful James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-20 20:15 -0700
                Re: Structured binding considered harmful jacobnavia <jacob@jacob.remcomp.fr> - 2020-03-21 08:32 +0100
                Re: Structured binding considered harmful David Brown <david.brown@hesbynett.no> - 2020-03-21 11:27 +0100
                Re: Structured binding considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2020-03-21 13:34 +0000
                Re: Structured binding considered harmful Daniel <danielaparker@gmail.com> - 2020-03-21 07:11 -0700
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-21 10:45 -0400
                Re: Structured binding considered harmful Daniel <danielaparker@gmail.com> - 2020-03-21 08:03 -0700
                Re: Structured binding considered harmful Daniel <danielaparker@gmail.com> - 2020-03-19 08:24 -0700
                Re: Structured binding considered harmful Ian Collins <ian-news@hotmail.com> - 2020-03-19 09:05 +1300
                Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-18 19:47 -0400
                Re: Structured binding considered harmful James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-18 11:43 -0700
                Re: Structured binding considered harmful Bonita Montero <Bonita.Montero@gmail.com> - 2020-03-18 19:48 +0100
                Re: Structured binding considered harmful James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-03-18 12:37 -0700
          Re: Structured binding considered harmful jacobnavia <jacob@jacob.remcomp.fr> - 2020-03-16 16:11 +0100
            Re: Structured binding considered harmful Ian Collins <ian-news@hotmail.com> - 2020-03-17 08:17 +1300
            Re: Structured binding considered harmful Daniel <danielaparker@gmail.com> - 2020-03-16 13:12 -0700
            Re: Structured binding considered harmful Sam <sam@email-scan.com> - 2020-03-16 20:13 -0400
  Re: Structured binding considered harmful Melzzzzz <Melzzzzz@zzzzz.com> - 2020-03-17 19:46 +0000
    Re: Structured binding considered harmful Daniel <danielaparker@gmail.com> - 2020-03-21 01:43 -0700

csiph-web