Why does const auto &pnullptr work while auto *pnullptr doesn't in C++17?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
21
down vote

favorite
1












This definition works:



const auto &bnullptr;


while this fails:



auto *bnullptr;


I have tried to compile this in Visual C++, GCC, and Clang. They all complain "cannot deduce type".



In the second case, shouldn't b be deduced to have some type like std::nullptr_t?










share|improve this question



















  • 2




    did you mean to use auto bnullptr; ?
    – Sander De Dycker
    yesterday










  • if you think the second bwould have type std::nullptr_t, what type do you think the first b has?
    – molbdnilo
    yesterday






  • 1




    Am I the only one that thinks std::nullptr_t b; is more readable?
    – rubenvb
    yesterday














up vote
21
down vote

favorite
1












This definition works:



const auto &bnullptr;


while this fails:



auto *bnullptr;


I have tried to compile this in Visual C++, GCC, and Clang. They all complain "cannot deduce type".



In the second case, shouldn't b be deduced to have some type like std::nullptr_t?










share|improve this question



















  • 2




    did you mean to use auto bnullptr; ?
    – Sander De Dycker
    yesterday










  • if you think the second bwould have type std::nullptr_t, what type do you think the first b has?
    – molbdnilo
    yesterday






  • 1




    Am I the only one that thinks std::nullptr_t b; is more readable?
    – rubenvb
    yesterday












up vote
21
down vote

favorite
1









up vote
21
down vote

favorite
1






1





This definition works:



const auto &bnullptr;


while this fails:



auto *bnullptr;


I have tried to compile this in Visual C++, GCC, and Clang. They all complain "cannot deduce type".



In the second case, shouldn't b be deduced to have some type like std::nullptr_t?










share|improve this question















This definition works:



const auto &bnullptr;


while this fails:



auto *bnullptr;


I have tried to compile this in Visual C++, GCC, and Clang. They all complain "cannot deduce type".



In the second case, shouldn't b be deduced to have some type like std::nullptr_t?







c++ c++17






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Peter Mortensen

12.9k1983111




12.9k1983111










asked yesterday









felix

929112




929112







  • 2




    did you mean to use auto bnullptr; ?
    – Sander De Dycker
    yesterday










  • if you think the second bwould have type std::nullptr_t, what type do you think the first b has?
    – molbdnilo
    yesterday






  • 1




    Am I the only one that thinks std::nullptr_t b; is more readable?
    – rubenvb
    yesterday












  • 2




    did you mean to use auto bnullptr; ?
    – Sander De Dycker
    yesterday










  • if you think the second bwould have type std::nullptr_t, what type do you think the first b has?
    – molbdnilo
    yesterday






  • 1




    Am I the only one that thinks std::nullptr_t b; is more readable?
    – rubenvb
    yesterday







2




2




did you mean to use auto bnullptr; ?
– Sander De Dycker
yesterday




did you mean to use auto bnullptr; ?
– Sander De Dycker
yesterday












if you think the second bwould have type std::nullptr_t, what type do you think the first b has?
– molbdnilo
yesterday




if you think the second bwould have type std::nullptr_t, what type do you think the first b has?
– molbdnilo
yesterday




1




1




Am I the only one that thinks std::nullptr_t b; is more readable?
– rubenvb
yesterday




Am I the only one that thinks std::nullptr_t b; is more readable?
– rubenvb
yesterday












3 Answers
3






active

oldest

votes

















up vote
30
down vote



accepted










It's because you declare b to be a pointer, and initialize it to be a null pointer. But a null pointer to what type of data you don't say, so the compiler can't deduce the type.



If you want b to be a std::nullptr_t object, you should drop the asterisk:



auto bnullptr;





share|improve this answer
















  • 21




    Ah, nullptr, the not-a-pointer pointer literal
    – Caleth
    yesterday






  • 2




    Thanks! I have just realized that nullptr has type std::nullptr_t which is not a pointer.... It is strange but make sense in this case.
    – felix
    yesterday






  • 4




    @felix It ceases to be strange when you take into account that nullptr is convertible to both all pointer types and also all member pointer types.
    – Angew
    yesterday

















up vote
12
down vote













decltype(nullptr) is std::nullptr_t.



so with



const auto &bnullptr; // auto is std::nullptr_t
// b is a reference to a temporary (with lifetime extension)


but nullptr is NOT a pointer (even if it is convertible to).



so auto *bnullptr; is invalid.



You might use instead



auto bnullptr; // auto is std::nullptr_t





share|improve this answer





























    up vote
    9
    down vote













    nullptr is of type std::nullptr_t. As a nullptr does not point to anything, there is no corresponding pointee type for std::nullptr_t (you are not allowed to dereference a nullptr), hence



    auto *b nullptr;


    requests a type that does not exist. If you want a to be of type nullptr_t simply write



    auto b nullptr;





    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',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      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%2f52274530%2fwhy-does-const-auto-pnullptr-work-while-auto-pnullptr-doesnt-in-c17%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      30
      down vote



      accepted










      It's because you declare b to be a pointer, and initialize it to be a null pointer. But a null pointer to what type of data you don't say, so the compiler can't deduce the type.



      If you want b to be a std::nullptr_t object, you should drop the asterisk:



      auto bnullptr;





      share|improve this answer
















      • 21




        Ah, nullptr, the not-a-pointer pointer literal
        – Caleth
        yesterday






      • 2




        Thanks! I have just realized that nullptr has type std::nullptr_t which is not a pointer.... It is strange but make sense in this case.
        – felix
        yesterday






      • 4




        @felix It ceases to be strange when you take into account that nullptr is convertible to both all pointer types and also all member pointer types.
        – Angew
        yesterday














      up vote
      30
      down vote



      accepted










      It's because you declare b to be a pointer, and initialize it to be a null pointer. But a null pointer to what type of data you don't say, so the compiler can't deduce the type.



      If you want b to be a std::nullptr_t object, you should drop the asterisk:



      auto bnullptr;





      share|improve this answer
















      • 21




        Ah, nullptr, the not-a-pointer pointer literal
        – Caleth
        yesterday






      • 2




        Thanks! I have just realized that nullptr has type std::nullptr_t which is not a pointer.... It is strange but make sense in this case.
        – felix
        yesterday






      • 4




        @felix It ceases to be strange when you take into account that nullptr is convertible to both all pointer types and also all member pointer types.
        – Angew
        yesterday












      up vote
      30
      down vote



      accepted







      up vote
      30
      down vote



      accepted






      It's because you declare b to be a pointer, and initialize it to be a null pointer. But a null pointer to what type of data you don't say, so the compiler can't deduce the type.



      If you want b to be a std::nullptr_t object, you should drop the asterisk:



      auto bnullptr;





      share|improve this answer












      It's because you declare b to be a pointer, and initialize it to be a null pointer. But a null pointer to what type of data you don't say, so the compiler can't deduce the type.



      If you want b to be a std::nullptr_t object, you should drop the asterisk:



      auto bnullptr;






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered yesterday









      Some programmer dude

      282k23229384




      282k23229384







      • 21




        Ah, nullptr, the not-a-pointer pointer literal
        – Caleth
        yesterday






      • 2




        Thanks! I have just realized that nullptr has type std::nullptr_t which is not a pointer.... It is strange but make sense in this case.
        – felix
        yesterday






      • 4




        @felix It ceases to be strange when you take into account that nullptr is convertible to both all pointer types and also all member pointer types.
        – Angew
        yesterday












      • 21




        Ah, nullptr, the not-a-pointer pointer literal
        – Caleth
        yesterday






      • 2




        Thanks! I have just realized that nullptr has type std::nullptr_t which is not a pointer.... It is strange but make sense in this case.
        – felix
        yesterday






      • 4




        @felix It ceases to be strange when you take into account that nullptr is convertible to both all pointer types and also all member pointer types.
        – Angew
        yesterday







      21




      21




      Ah, nullptr, the not-a-pointer pointer literal
      – Caleth
      yesterday




      Ah, nullptr, the not-a-pointer pointer literal
      – Caleth
      yesterday




      2




      2




      Thanks! I have just realized that nullptr has type std::nullptr_t which is not a pointer.... It is strange but make sense in this case.
      – felix
      yesterday




      Thanks! I have just realized that nullptr has type std::nullptr_t which is not a pointer.... It is strange but make sense in this case.
      – felix
      yesterday




      4




      4




      @felix It ceases to be strange when you take into account that nullptr is convertible to both all pointer types and also all member pointer types.
      – Angew
      yesterday




      @felix It ceases to be strange when you take into account that nullptr is convertible to both all pointer types and also all member pointer types.
      – Angew
      yesterday












      up vote
      12
      down vote













      decltype(nullptr) is std::nullptr_t.



      so with



      const auto &bnullptr; // auto is std::nullptr_t
      // b is a reference to a temporary (with lifetime extension)


      but nullptr is NOT a pointer (even if it is convertible to).



      so auto *bnullptr; is invalid.



      You might use instead



      auto bnullptr; // auto is std::nullptr_t





      share|improve this answer


























        up vote
        12
        down vote













        decltype(nullptr) is std::nullptr_t.



        so with



        const auto &bnullptr; // auto is std::nullptr_t
        // b is a reference to a temporary (with lifetime extension)


        but nullptr is NOT a pointer (even if it is convertible to).



        so auto *bnullptr; is invalid.



        You might use instead



        auto bnullptr; // auto is std::nullptr_t





        share|improve this answer
























          up vote
          12
          down vote










          up vote
          12
          down vote









          decltype(nullptr) is std::nullptr_t.



          so with



          const auto &bnullptr; // auto is std::nullptr_t
          // b is a reference to a temporary (with lifetime extension)


          but nullptr is NOT a pointer (even if it is convertible to).



          so auto *bnullptr; is invalid.



          You might use instead



          auto bnullptr; // auto is std::nullptr_t





          share|improve this answer














          decltype(nullptr) is std::nullptr_t.



          so with



          const auto &bnullptr; // auto is std::nullptr_t
          // b is a reference to a temporary (with lifetime extension)


          but nullptr is NOT a pointer (even if it is convertible to).



          so auto *bnullptr; is invalid.



          You might use instead



          auto bnullptr; // auto is std::nullptr_t






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered yesterday









          Jarod42

          106k1296168




          106k1296168




















              up vote
              9
              down vote













              nullptr is of type std::nullptr_t. As a nullptr does not point to anything, there is no corresponding pointee type for std::nullptr_t (you are not allowed to dereference a nullptr), hence



              auto *b nullptr;


              requests a type that does not exist. If you want a to be of type nullptr_t simply write



              auto b nullptr;





              share|improve this answer


























                up vote
                9
                down vote













                nullptr is of type std::nullptr_t. As a nullptr does not point to anything, there is no corresponding pointee type for std::nullptr_t (you are not allowed to dereference a nullptr), hence



                auto *b nullptr;


                requests a type that does not exist. If you want a to be of type nullptr_t simply write



                auto b nullptr;





                share|improve this answer
























                  up vote
                  9
                  down vote










                  up vote
                  9
                  down vote









                  nullptr is of type std::nullptr_t. As a nullptr does not point to anything, there is no corresponding pointee type for std::nullptr_t (you are not allowed to dereference a nullptr), hence



                  auto *b nullptr;


                  requests a type that does not exist. If you want a to be of type nullptr_t simply write



                  auto b nullptr;





                  share|improve this answer














                  nullptr is of type std::nullptr_t. As a nullptr does not point to anything, there is no corresponding pointee type for std::nullptr_t (you are not allowed to dereference a nullptr), hence



                  auto *b nullptr;


                  requests a type that does not exist. If you want a to be of type nullptr_t simply write



                  auto b nullptr;






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited yesterday

























                  answered yesterday









                  user463035818

                  14k22256




                  14k22256



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52274530%2fwhy-does-const-auto-pnullptr-work-while-auto-pnullptr-doesnt-in-c17%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      Installing NextGIS Connect into QGIS 3?

                      One-line joke