Structured binding on constHow to convert a std::string to const char* or char*?What is the difference...

Lightning Web Component - do I need to track changes for every single input field in a form

How to check participants in at events?

What is the term when two people sing in harmony, but they aren't singing the same notes?

Is it okay / does it make sense for another player to join a running game of Munchkin?

Java - What do constructor type arguments mean when placed *before* the type?

Organic chemistry Iodoform Reaction

For airliners, what prevents wing strikes on landing in bad weather?

Visiting the UK as unmarried couple

Teaching indefinite integrals that require special-casing

Stereotypical names

What to do when my ideas aren't chosen, when I strongly disagree with the chosen solution?

What (else) happened July 1st 1858 in London?

Female=gender counterpart?

Perfect riffle shuffles

In Star Trek IV, why did the Bounty go back to a time when whales were already rare?

I'm in charge of equipment buying but no one's ever happy with what I choose. How to fix this?

What will be the temperature on Earth when Sun finishes its main sequence?

Books on the History of math research at European universities

Can I rely on these GitHub repository files?

Can a Gentile theist be saved?

Simple image editor tool to draw a simple box/rectangle in an existing image

How do ultrasonic sensors differentiate between transmitted and received signals?

Are taller landing gear bad for aircraft, particulary large airliners?

Are Warlocks Arcane or Divine?



Structured binding on const


How to convert a std::string to const char* or char*?What is the difference between const int*, const int * const, and int const *?const pointers in overload resolutionString literal in templates - different behavior of compilersConfirm This Standard Library Bug Relating to Allocators in MSVC 2015 RCCapturing array of vectors in lambda makes elements constExplicit destructor call with decltypeWhy “int & const” compiles fine with MSVC?Visual accept std::string from std::byte iteratorShall structured binding to a copy of a const c-array be const?













10















Is the following code supposed to compile?



#include <type_traits>

void foo() {
const std::pair<int, int> x = {1, 2};

auto [a, b] = x;

static_assert(std::is_const_v<decltype(a)>);
static_assert(std::is_const_v<decltype(b)>);
}




  • MSVC says "yes!".


  • GCC says "oh no, man!".


  • Clang says "no way!".




So, is this an MSVC bug?



The standard is not straightforward here (I had a quick look), but considering the rules for auto, I suppose, a and b should be copied discarding cv-qualifier.










share|improve this question





























    10















    Is the following code supposed to compile?



    #include <type_traits>

    void foo() {
    const std::pair<int, int> x = {1, 2};

    auto [a, b] = x;

    static_assert(std::is_const_v<decltype(a)>);
    static_assert(std::is_const_v<decltype(b)>);
    }




    • MSVC says "yes!".


    • GCC says "oh no, man!".


    • Clang says "no way!".




    So, is this an MSVC bug?



    The standard is not straightforward here (I had a quick look), but considering the rules for auto, I suppose, a and b should be copied discarding cv-qualifier.










    share|improve this question



























      10












      10








      10








      Is the following code supposed to compile?



      #include <type_traits>

      void foo() {
      const std::pair<int, int> x = {1, 2};

      auto [a, b] = x;

      static_assert(std::is_const_v<decltype(a)>);
      static_assert(std::is_const_v<decltype(b)>);
      }




      • MSVC says "yes!".


      • GCC says "oh no, man!".


      • Clang says "no way!".




      So, is this an MSVC bug?



      The standard is not straightforward here (I had a quick look), but considering the rules for auto, I suppose, a and b should be copied discarding cv-qualifier.










      share|improve this question
















      Is the following code supposed to compile?



      #include <type_traits>

      void foo() {
      const std::pair<int, int> x = {1, 2};

      auto [a, b] = x;

      static_assert(std::is_const_v<decltype(a)>);
      static_assert(std::is_const_v<decltype(b)>);
      }




      • MSVC says "yes!".


      • GCC says "oh no, man!".


      • Clang says "no way!".




      So, is this an MSVC bug?



      The standard is not straightforward here (I had a quick look), but considering the rules for auto, I suppose, a and b should be copied discarding cv-qualifier.







      c++ c++17 structured-bindings






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 15 hours ago









      einpoklum

      36.2k28132260




      36.2k28132260










      asked yesterday









      Biagio FestaBiagio Festa

      5,21021240




      5,21021240
























          2 Answers
          2






          active

          oldest

          votes


















          12















          Is the following code supposed to compile?




          It is not. This is an MSVC bug.



          A structured binding declaration introduces a new name (for specification only), e, that is declared like:



          auto e = x;


          The type of e is called E, and since the initializer is tuple-like, the types of the bindings are given by tuple_element_t<i, E>. In this case E is pair<int, int>, so the two types are just int. The rule for decltype of a structured binding is to give the referenced type, so decltype(a) and decltype(b) are both int.



          The important part here is that a and b (the structured bindings) come from the invented variable (e), and not its initializer (x). e is not const because you just declared it auto. What we're doing is copying x, and then taking bindings into this (non-const) copy.






          share|improve this answer































            7














            The static assertions in your code should fail. Why? Because your code is basically the same as the case of:



            #include <type_traits>

            void foo() {
            const int x_1 = 1;
            const int x_2 = 2;

            auto a = x_1;
            auto b = x_2;

            static_assert(std::is_const_v<decltype(a)>);
            static_assert(std::is_const_v<decltype(b)>);
            }


            which does indeed fail on MSVC as well.



            In C++ expression types decay on assignment: the auto sees an int, not a const int. Structured binding simply lets you do more than a single auto binding at a time.



            ... and so the fact that MSVC doesn't fail on the assertions in your code seems to be a bug.






            share|improve this answer

























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55329651%2fstructured-binding-on-const%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              12















              Is the following code supposed to compile?




              It is not. This is an MSVC bug.



              A structured binding declaration introduces a new name (for specification only), e, that is declared like:



              auto e = x;


              The type of e is called E, and since the initializer is tuple-like, the types of the bindings are given by tuple_element_t<i, E>. In this case E is pair<int, int>, so the two types are just int. The rule for decltype of a structured binding is to give the referenced type, so decltype(a) and decltype(b) are both int.



              The important part here is that a and b (the structured bindings) come from the invented variable (e), and not its initializer (x). e is not const because you just declared it auto. What we're doing is copying x, and then taking bindings into this (non-const) copy.






              share|improve this answer




























                12















                Is the following code supposed to compile?




                It is not. This is an MSVC bug.



                A structured binding declaration introduces a new name (for specification only), e, that is declared like:



                auto e = x;


                The type of e is called E, and since the initializer is tuple-like, the types of the bindings are given by tuple_element_t<i, E>. In this case E is pair<int, int>, so the two types are just int. The rule for decltype of a structured binding is to give the referenced type, so decltype(a) and decltype(b) are both int.



                The important part here is that a and b (the structured bindings) come from the invented variable (e), and not its initializer (x). e is not const because you just declared it auto. What we're doing is copying x, and then taking bindings into this (non-const) copy.






                share|improve this answer


























                  12












                  12








                  12








                  Is the following code supposed to compile?




                  It is not. This is an MSVC bug.



                  A structured binding declaration introduces a new name (for specification only), e, that is declared like:



                  auto e = x;


                  The type of e is called E, and since the initializer is tuple-like, the types of the bindings are given by tuple_element_t<i, E>. In this case E is pair<int, int>, so the two types are just int. The rule for decltype of a structured binding is to give the referenced type, so decltype(a) and decltype(b) are both int.



                  The important part here is that a and b (the structured bindings) come from the invented variable (e), and not its initializer (x). e is not const because you just declared it auto. What we're doing is copying x, and then taking bindings into this (non-const) copy.






                  share|improve this answer














                  Is the following code supposed to compile?




                  It is not. This is an MSVC bug.



                  A structured binding declaration introduces a new name (for specification only), e, that is declared like:



                  auto e = x;


                  The type of e is called E, and since the initializer is tuple-like, the types of the bindings are given by tuple_element_t<i, E>. In this case E is pair<int, int>, so the two types are just int. The rule for decltype of a structured binding is to give the referenced type, so decltype(a) and decltype(b) are both int.



                  The important part here is that a and b (the structured bindings) come from the invented variable (e), and not its initializer (x). e is not const because you just declared it auto. What we're doing is copying x, and then taking bindings into this (non-const) copy.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered yesterday









                  BarryBarry

                  185k21326601




                  185k21326601

























                      7














                      The static assertions in your code should fail. Why? Because your code is basically the same as the case of:



                      #include <type_traits>

                      void foo() {
                      const int x_1 = 1;
                      const int x_2 = 2;

                      auto a = x_1;
                      auto b = x_2;

                      static_assert(std::is_const_v<decltype(a)>);
                      static_assert(std::is_const_v<decltype(b)>);
                      }


                      which does indeed fail on MSVC as well.



                      In C++ expression types decay on assignment: the auto sees an int, not a const int. Structured binding simply lets you do more than a single auto binding at a time.



                      ... and so the fact that MSVC doesn't fail on the assertions in your code seems to be a bug.






                      share|improve this answer






























                        7














                        The static assertions in your code should fail. Why? Because your code is basically the same as the case of:



                        #include <type_traits>

                        void foo() {
                        const int x_1 = 1;
                        const int x_2 = 2;

                        auto a = x_1;
                        auto b = x_2;

                        static_assert(std::is_const_v<decltype(a)>);
                        static_assert(std::is_const_v<decltype(b)>);
                        }


                        which does indeed fail on MSVC as well.



                        In C++ expression types decay on assignment: the auto sees an int, not a const int. Structured binding simply lets you do more than a single auto binding at a time.



                        ... and so the fact that MSVC doesn't fail on the assertions in your code seems to be a bug.






                        share|improve this answer




























                          7












                          7








                          7







                          The static assertions in your code should fail. Why? Because your code is basically the same as the case of:



                          #include <type_traits>

                          void foo() {
                          const int x_1 = 1;
                          const int x_2 = 2;

                          auto a = x_1;
                          auto b = x_2;

                          static_assert(std::is_const_v<decltype(a)>);
                          static_assert(std::is_const_v<decltype(b)>);
                          }


                          which does indeed fail on MSVC as well.



                          In C++ expression types decay on assignment: the auto sees an int, not a const int. Structured binding simply lets you do more than a single auto binding at a time.



                          ... and so the fact that MSVC doesn't fail on the assertions in your code seems to be a bug.






                          share|improve this answer















                          The static assertions in your code should fail. Why? Because your code is basically the same as the case of:



                          #include <type_traits>

                          void foo() {
                          const int x_1 = 1;
                          const int x_2 = 2;

                          auto a = x_1;
                          auto b = x_2;

                          static_assert(std::is_const_v<decltype(a)>);
                          static_assert(std::is_const_v<decltype(b)>);
                          }


                          which does indeed fail on MSVC as well.



                          In C++ expression types decay on assignment: the auto sees an int, not a const int. Structured binding simply lets you do more than a single auto binding at a time.



                          ... and so the fact that MSVC doesn't fail on the assertions in your code seems to be a bug.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 6 hours ago

























                          answered yesterday









                          einpoklumeinpoklum

                          36.2k28132260




                          36.2k28132260






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55329651%2fstructured-binding-on-const%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

                              Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

                              Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...