Structured binding on constWhat is the difference between const int*, const int * const, and int const *?String literal in templates - different behavior of compilersstructured binding with [[maybe_unused]]Capturing array of vectors in lambda makes elements constExplicit destructor call with decltypeWhy “int & const” compiles fine with MSVC?Discards qualifiers unknown cause (std::bind() / lambda)Visual accept std::string from std::byte iteratorC++17 - Binding rvalue reference to non-const lvalue refShall structured binding to a copy of a const c-array be const?

Is a model fitted to data or is data fitted to a model?

How should I respond when I lied about my education and the company finds out through background check?

Can I use my Chinese passport to enter China after I acquired another citizenship?

Drawing ramified coverings with tikz

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

How do ground effect vehicles perform turns?

Transformation of random variables and joint distributions

Folder comparison

Journal losing indexing services

Customize circled numbers

How to color a curve

Confusion on Parallelogram

My friend sent me a screenshot of a transaction hash, but when I search for it I find divergent data. What happened?

What does the Rambam mean when he says that the planets have souls?

Are all species of CANNA edible?

Why is Arduino resetting while driving motors?

Will adding a BY-SA image to a blog post make the entire post BY-SA?

Proof of Lemma: Every nonzero integer can be written as a product of primes

Flux received by a negative charge

Is camera lens focus an exact point or a range?

Can a significant change in incentives void an employment contract?

THT: What is a squared annular “ring”?

How do I extrude a face to a single vertex

Is it improper etiquette to ask your opponent what his/her rating is before the game?



Structured binding on const


What is the difference between const int*, const int * const, and int const *?String literal in templates - different behavior of compilersstructured binding with [[maybe_unused]]Capturing array of vectors in lambda makes elements constExplicit destructor call with decltypeWhy “int & const” compiles fine with MSVC?Discards qualifiers unknown cause (std::bind() / lambda)Visual accept std::string from std::byte iteratorC++17 - Binding rvalue reference to non-const lvalue refShall structured binding to a copy of a const c-array be const?













7















Is the following code supposed to compile?



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


























    7















    Is the following code supposed to compile?



    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
























      7












      7








      7








      Is the following code supposed to compile?



      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?



      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










      asked 6 hours ago









      Biagio FestaBiagio Festa

      5,19321239




      5,19321239






















          2 Answers
          2






          active

          oldest

          votes


















          8















          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






























            3














            The static assertions should fail (so this would be an MSVC bug I guess). Why? Because it's basically the same as the case of:



            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.






            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









              8















              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



























                8















                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

























                  8












                  8








                  8








                  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 6 hours ago









                  BarryBarry

                  185k21325600




                  185k21325600























                      3














                      The static assertions should fail (so this would be an MSVC bug I guess). Why? Because it's basically the same as the case of:



                      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.






                      share|improve this answer





























                        3














                        The static assertions should fail (so this would be an MSVC bug I guess). Why? Because it's basically the same as the case of:



                        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.






                        share|improve this answer



























                          3












                          3








                          3







                          The static assertions should fail (so this would be an MSVC bug I guess). Why? Because it's basically the same as the case of:



                          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.






                          share|improve this answer















                          The static assertions should fail (so this would be an MSVC bug I guess). Why? Because it's basically the same as the case of:



                          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.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 6 hours ago

























                          answered 6 hours ago









                          einpoklumeinpoklum

                          36.1k28132260




                          36.1k28132260



























                              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

                              कुँवर स्रोत दिक्चालन सूची"कुँवर""राणा कुँवरके वंशावली"

                              Why is a white electrical wire connected to 2 black wires?How to wire a light fixture with 3 white wires in box?How should I wire a ceiling fan when there's only three wires in the box?Two white, two black, two ground, and red wire in ceiling box connected to switchWhy is there a white wire connected to multiple black wires in my light box?How to wire a light with two white wires and one black wireReplace light switch connected to a power outlet with dimmer - two black wires to one black and redHow to wire a light with multiple black/white/green wires from the ceiling?Ceiling box has 2 black and white wires but fan/ light only has 1 of eachWhy neutral wire connected to load wire?Switch with 2 black, 2 white, 2 ground and 1 red wire connected to ceiling light and a receptacle?

                              चैत्य भूमि चित्र दीर्घा सन्दर्भ बाहरी कडियाँ दिक्चालन सूची"Chaitya Bhoomi""Chaitya Bhoomi: Statue of Equality in India""Dadar Chaitya Bhoomi: Statue of Equality in India""Ambedkar memorial: Centre okays transfer of Indu Mill land"चैत्यभमि