What's the difference between `auto pp` and `auto *ppp`?











up vote
13
down vote

favorite
2












int foo = 11;
int *p = &foo;

auto pp = p;
auto *ppp = p;

cout << pp << endl;
cout << ppp << endl;


This program will produce the same output for pp and ppp, but why? auto deduces the variable should be int, so I think the declaration of ppp is right. But pp and ppp have the same value...



Output:



0x61fefc
0x61fefc









share|improve this question




























    up vote
    13
    down vote

    favorite
    2












    int foo = 11;
    int *p = &foo;

    auto pp = p;
    auto *ppp = p;

    cout << pp << endl;
    cout << ppp << endl;


    This program will produce the same output for pp and ppp, but why? auto deduces the variable should be int, so I think the declaration of ppp is right. But pp and ppp have the same value...



    Output:



    0x61fefc
    0x61fefc









    share|improve this question


























      up vote
      13
      down vote

      favorite
      2









      up vote
      13
      down vote

      favorite
      2






      2





      int foo = 11;
      int *p = &foo;

      auto pp = p;
      auto *ppp = p;

      cout << pp << endl;
      cout << ppp << endl;


      This program will produce the same output for pp and ppp, but why? auto deduces the variable should be int, so I think the declaration of ppp is right. But pp and ppp have the same value...



      Output:



      0x61fefc
      0x61fefc









      share|improve this question















      int foo = 11;
      int *p = &foo;

      auto pp = p;
      auto *ppp = p;

      cout << pp << endl;
      cout << ppp << endl;


      This program will produce the same output for pp and ppp, but why? auto deduces the variable should be int, so I think the declaration of ppp is right. But pp and ppp have the same value...



      Output:



      0x61fefc
      0x61fefc






      c++ c++11 pointers auto






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 29 at 17:40









      Boann

      36.5k1287120




      36.5k1287120










      asked Nov 29 at 10:50









      廖茂生

      978




      978
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          23
          down vote













          In the particular example you show, there is no difference. But imagine you would later on add two const qualifier like the following:



          const auto pp = p;
          const auto *ppp = p;


          Is it still the same? Turns out that this is identical to



          int * const pp = p; // pointer is readonly
          const int *ppp = p; // pointee is readonly


          because in auto pp = p, auto matches int* as a whole, and const modifies what's on its left (or what's on its right, if there is nothing on its left). Contrary, in auto *ppp = p, auto matches int, and this is what const applies to.



          Because of this notable difference and because we should use const variables whenever possible, I'd advise you to always use auto* when using type deduction for pointer variables. There is no way to const-qualify the pointer itself instead of the pointee, and if you want to const-qualify both, this is possible by



          const auto * const pppp = p;


          which doesn't work without the *.






          share|improve this answer























          • Subtle, and important to know, and I don't recall reading this in any recent C++ book, e.g., Stroustrup's Tour 2nd ed. (I may have missed it.)
            – davidbak
            Nov 29 at 15:10










          • Actually you didn't add const qualifier to both variables. You instead added it to the first variable and to the pointee of the second one.
            – Ruslan
            Nov 29 at 15:13










          • @Ruslan You're right, I'll improve the wording.
            – lubgr
            Nov 29 at 15:13






          • 2




            @davidbak I agree, this is not super obvious. It bit me once, and as painful learning is quite effective, I remember since then :)
            – lubgr
            Nov 29 at 15:14








          • 2




            Yepppp! Painful learning is quite effective but unfortunately time consuming (and sometimes frustrating). Reading a nice explanation (making things obvious) is the 2nd best option (and much less time consuming).
            – Scheff
            2 days ago


















          up vote
          6
          down vote













          There is no difference in auto and auto * in this particular case. In case of auto pp = p; type will be deduced to int * while in case of auto *ppp = p; type will be deduced to int.



          auto qualifier:




          For variables, specifies that the type of the variable that is being declared will be automatically deduced from its initializer. [...]




          Note that unlike auto the auto * will deduce only pointer types.






          share|improve this answer



















          • 6




            The key point is auto uses template argument deduction rules, and so auto* will only deduce pointer types.
            – rustyx
            Nov 29 at 10:58










          • @rustyx; I added particular case.
            – haccks
            Nov 29 at 11:00











          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',
          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%2f53537326%2fwhats-the-difference-between-auto-pp-and-auto-ppp%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








          up vote
          23
          down vote













          In the particular example you show, there is no difference. But imagine you would later on add two const qualifier like the following:



          const auto pp = p;
          const auto *ppp = p;


          Is it still the same? Turns out that this is identical to



          int * const pp = p; // pointer is readonly
          const int *ppp = p; // pointee is readonly


          because in auto pp = p, auto matches int* as a whole, and const modifies what's on its left (or what's on its right, if there is nothing on its left). Contrary, in auto *ppp = p, auto matches int, and this is what const applies to.



          Because of this notable difference and because we should use const variables whenever possible, I'd advise you to always use auto* when using type deduction for pointer variables. There is no way to const-qualify the pointer itself instead of the pointee, and if you want to const-qualify both, this is possible by



          const auto * const pppp = p;


          which doesn't work without the *.






          share|improve this answer























          • Subtle, and important to know, and I don't recall reading this in any recent C++ book, e.g., Stroustrup's Tour 2nd ed. (I may have missed it.)
            – davidbak
            Nov 29 at 15:10










          • Actually you didn't add const qualifier to both variables. You instead added it to the first variable and to the pointee of the second one.
            – Ruslan
            Nov 29 at 15:13










          • @Ruslan You're right, I'll improve the wording.
            – lubgr
            Nov 29 at 15:13






          • 2




            @davidbak I agree, this is not super obvious. It bit me once, and as painful learning is quite effective, I remember since then :)
            – lubgr
            Nov 29 at 15:14








          • 2




            Yepppp! Painful learning is quite effective but unfortunately time consuming (and sometimes frustrating). Reading a nice explanation (making things obvious) is the 2nd best option (and much less time consuming).
            – Scheff
            2 days ago















          up vote
          23
          down vote













          In the particular example you show, there is no difference. But imagine you would later on add two const qualifier like the following:



          const auto pp = p;
          const auto *ppp = p;


          Is it still the same? Turns out that this is identical to



          int * const pp = p; // pointer is readonly
          const int *ppp = p; // pointee is readonly


          because in auto pp = p, auto matches int* as a whole, and const modifies what's on its left (or what's on its right, if there is nothing on its left). Contrary, in auto *ppp = p, auto matches int, and this is what const applies to.



          Because of this notable difference and because we should use const variables whenever possible, I'd advise you to always use auto* when using type deduction for pointer variables. There is no way to const-qualify the pointer itself instead of the pointee, and if you want to const-qualify both, this is possible by



          const auto * const pppp = p;


          which doesn't work without the *.






          share|improve this answer























          • Subtle, and important to know, and I don't recall reading this in any recent C++ book, e.g., Stroustrup's Tour 2nd ed. (I may have missed it.)
            – davidbak
            Nov 29 at 15:10










          • Actually you didn't add const qualifier to both variables. You instead added it to the first variable and to the pointee of the second one.
            – Ruslan
            Nov 29 at 15:13










          • @Ruslan You're right, I'll improve the wording.
            – lubgr
            Nov 29 at 15:13






          • 2




            @davidbak I agree, this is not super obvious. It bit me once, and as painful learning is quite effective, I remember since then :)
            – lubgr
            Nov 29 at 15:14








          • 2




            Yepppp! Painful learning is quite effective but unfortunately time consuming (and sometimes frustrating). Reading a nice explanation (making things obvious) is the 2nd best option (and much less time consuming).
            – Scheff
            2 days ago













          up vote
          23
          down vote










          up vote
          23
          down vote









          In the particular example you show, there is no difference. But imagine you would later on add two const qualifier like the following:



          const auto pp = p;
          const auto *ppp = p;


          Is it still the same? Turns out that this is identical to



          int * const pp = p; // pointer is readonly
          const int *ppp = p; // pointee is readonly


          because in auto pp = p, auto matches int* as a whole, and const modifies what's on its left (or what's on its right, if there is nothing on its left). Contrary, in auto *ppp = p, auto matches int, and this is what const applies to.



          Because of this notable difference and because we should use const variables whenever possible, I'd advise you to always use auto* when using type deduction for pointer variables. There is no way to const-qualify the pointer itself instead of the pointee, and if you want to const-qualify both, this is possible by



          const auto * const pppp = p;


          which doesn't work without the *.






          share|improve this answer














          In the particular example you show, there is no difference. But imagine you would later on add two const qualifier like the following:



          const auto pp = p;
          const auto *ppp = p;


          Is it still the same? Turns out that this is identical to



          int * const pp = p; // pointer is readonly
          const int *ppp = p; // pointee is readonly


          because in auto pp = p, auto matches int* as a whole, and const modifies what's on its left (or what's on its right, if there is nothing on its left). Contrary, in auto *ppp = p, auto matches int, and this is what const applies to.



          Because of this notable difference and because we should use const variables whenever possible, I'd advise you to always use auto* when using type deduction for pointer variables. There is no way to const-qualify the pointer itself instead of the pointee, and if you want to const-qualify both, this is possible by



          const auto * const pppp = p;


          which doesn't work without the *.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 29 at 15:14

























          answered Nov 29 at 11:33









          lubgr

          9,86121544




          9,86121544












          • Subtle, and important to know, and I don't recall reading this in any recent C++ book, e.g., Stroustrup's Tour 2nd ed. (I may have missed it.)
            – davidbak
            Nov 29 at 15:10










          • Actually you didn't add const qualifier to both variables. You instead added it to the first variable and to the pointee of the second one.
            – Ruslan
            Nov 29 at 15:13










          • @Ruslan You're right, I'll improve the wording.
            – lubgr
            Nov 29 at 15:13






          • 2




            @davidbak I agree, this is not super obvious. It bit me once, and as painful learning is quite effective, I remember since then :)
            – lubgr
            Nov 29 at 15:14








          • 2




            Yepppp! Painful learning is quite effective but unfortunately time consuming (and sometimes frustrating). Reading a nice explanation (making things obvious) is the 2nd best option (and much less time consuming).
            – Scheff
            2 days ago


















          • Subtle, and important to know, and I don't recall reading this in any recent C++ book, e.g., Stroustrup's Tour 2nd ed. (I may have missed it.)
            – davidbak
            Nov 29 at 15:10










          • Actually you didn't add const qualifier to both variables. You instead added it to the first variable and to the pointee of the second one.
            – Ruslan
            Nov 29 at 15:13










          • @Ruslan You're right, I'll improve the wording.
            – lubgr
            Nov 29 at 15:13






          • 2




            @davidbak I agree, this is not super obvious. It bit me once, and as painful learning is quite effective, I remember since then :)
            – lubgr
            Nov 29 at 15:14








          • 2




            Yepppp! Painful learning is quite effective but unfortunately time consuming (and sometimes frustrating). Reading a nice explanation (making things obvious) is the 2nd best option (and much less time consuming).
            – Scheff
            2 days ago
















          Subtle, and important to know, and I don't recall reading this in any recent C++ book, e.g., Stroustrup's Tour 2nd ed. (I may have missed it.)
          – davidbak
          Nov 29 at 15:10




          Subtle, and important to know, and I don't recall reading this in any recent C++ book, e.g., Stroustrup's Tour 2nd ed. (I may have missed it.)
          – davidbak
          Nov 29 at 15:10












          Actually you didn't add const qualifier to both variables. You instead added it to the first variable and to the pointee of the second one.
          – Ruslan
          Nov 29 at 15:13




          Actually you didn't add const qualifier to both variables. You instead added it to the first variable and to the pointee of the second one.
          – Ruslan
          Nov 29 at 15:13












          @Ruslan You're right, I'll improve the wording.
          – lubgr
          Nov 29 at 15:13




          @Ruslan You're right, I'll improve the wording.
          – lubgr
          Nov 29 at 15:13




          2




          2




          @davidbak I agree, this is not super obvious. It bit me once, and as painful learning is quite effective, I remember since then :)
          – lubgr
          Nov 29 at 15:14






          @davidbak I agree, this is not super obvious. It bit me once, and as painful learning is quite effective, I remember since then :)
          – lubgr
          Nov 29 at 15:14






          2




          2




          Yepppp! Painful learning is quite effective but unfortunately time consuming (and sometimes frustrating). Reading a nice explanation (making things obvious) is the 2nd best option (and much less time consuming).
          – Scheff
          2 days ago




          Yepppp! Painful learning is quite effective but unfortunately time consuming (and sometimes frustrating). Reading a nice explanation (making things obvious) is the 2nd best option (and much less time consuming).
          – Scheff
          2 days ago












          up vote
          6
          down vote













          There is no difference in auto and auto * in this particular case. In case of auto pp = p; type will be deduced to int * while in case of auto *ppp = p; type will be deduced to int.



          auto qualifier:




          For variables, specifies that the type of the variable that is being declared will be automatically deduced from its initializer. [...]




          Note that unlike auto the auto * will deduce only pointer types.






          share|improve this answer



















          • 6




            The key point is auto uses template argument deduction rules, and so auto* will only deduce pointer types.
            – rustyx
            Nov 29 at 10:58










          • @rustyx; I added particular case.
            – haccks
            Nov 29 at 11:00















          up vote
          6
          down vote













          There is no difference in auto and auto * in this particular case. In case of auto pp = p; type will be deduced to int * while in case of auto *ppp = p; type will be deduced to int.



          auto qualifier:




          For variables, specifies that the type of the variable that is being declared will be automatically deduced from its initializer. [...]




          Note that unlike auto the auto * will deduce only pointer types.






          share|improve this answer



















          • 6




            The key point is auto uses template argument deduction rules, and so auto* will only deduce pointer types.
            – rustyx
            Nov 29 at 10:58










          • @rustyx; I added particular case.
            – haccks
            Nov 29 at 11:00













          up vote
          6
          down vote










          up vote
          6
          down vote









          There is no difference in auto and auto * in this particular case. In case of auto pp = p; type will be deduced to int * while in case of auto *ppp = p; type will be deduced to int.



          auto qualifier:




          For variables, specifies that the type of the variable that is being declared will be automatically deduced from its initializer. [...]




          Note that unlike auto the auto * will deduce only pointer types.






          share|improve this answer














          There is no difference in auto and auto * in this particular case. In case of auto pp = p; type will be deduced to int * while in case of auto *ppp = p; type will be deduced to int.



          auto qualifier:




          For variables, specifies that the type of the variable that is being declared will be automatically deduced from its initializer. [...]




          Note that unlike auto the auto * will deduce only pointer types.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 29 at 17:49

























          answered Nov 29 at 10:52









          haccks

          85.4k20125216




          85.4k20125216








          • 6




            The key point is auto uses template argument deduction rules, and so auto* will only deduce pointer types.
            – rustyx
            Nov 29 at 10:58










          • @rustyx; I added particular case.
            – haccks
            Nov 29 at 11:00














          • 6




            The key point is auto uses template argument deduction rules, and so auto* will only deduce pointer types.
            – rustyx
            Nov 29 at 10:58










          • @rustyx; I added particular case.
            – haccks
            Nov 29 at 11:00








          6




          6




          The key point is auto uses template argument deduction rules, and so auto* will only deduce pointer types.
          – rustyx
          Nov 29 at 10:58




          The key point is auto uses template argument deduction rules, and so auto* will only deduce pointer types.
          – rustyx
          Nov 29 at 10:58












          @rustyx; I added particular case.
          – haccks
          Nov 29 at 11:00




          @rustyx; I added particular case.
          – haccks
          Nov 29 at 11:00


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53537326%2fwhats-the-difference-between-auto-pp-and-auto-ppp%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

          AnyDesk - Fatal Program Failure

          How to calibrate 16:9 built-in touch-screen to a 4:3 resolution?

          QoS: MAC-Priority for clients behind a repeater