Is the address of a local variable a constexpr? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag?What are the differences between a pointer variable and a reference variable in C++?When should you use constexpr capability in C++11?Undefined reference to static constexpr char[]const vs constexpr on variablesDoes static constexpr variable make sense?Difference between `constexpr` and `const`Unique address for constexpr variableenum vs constexpr for actual static constants inside classesConstexpr placement new?constexpr unique id, compiles with clang but not with gcc

List *all* the tuples!

Should I discuss the type of campaign with my players?

Why is black pepper both grey and black?

If a contract sometimes uses the wrong name, is it still valid?

Does surprise arrest existing movement?

How does a Death Domain cleric's Touch of Death feature work with Touch-range spells delivered by familiars?

3 doors, three guards, one stone

How can I make names more distinctive without making them longer?

What does the "x" in "x86" represent?

Can a non-EU citizen traveling with me come with me through the EU passport line?

What is the musical term for a note that continously plays through a melody?

What do you call a phrase that's not an idiom yet?

Is there a concise way to say "all of the X, one of each"?

Why was the term "discrete" used in discrete logarithm?

Is there a documented rationale why the House Ways and Means chairman can demand tax info?

Is a manifold-with-boundary with given interior and non-empty boundary essentially unique?

Do I really need recursive chmod to restrict access to a folder?

Is 1 ppb equal to 1 μg/kg?

Can Pao de Queijo, and similar foods, be kosher for Passover?

Why is "Captain Marvel" translated as male in Portugal?

When -s is used with third person singular. What's its use in this context?

When to stop saving and start investing?

Check which numbers satisfy the condition [A*B*C = A! + B! + C!]

Why does Python start at index -1 when indexing a list from the end?



Is the address of a local variable a constexpr?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?What are the differences between a pointer variable and a reference variable in C++?When should you use constexpr capability in C++11?Undefined reference to static constexpr char[]const vs constexpr on variablesDoes static constexpr variable make sense?Difference between `constexpr` and `const`Unique address for constexpr variableenum vs constexpr for actual static constants inside classesConstexpr placement new?constexpr unique id, compiles with clang but not with gcc



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








10















In Bjarne Stroustrup's book "The C++ Programming Language (4th Edition)" on p. 267 (Section 10.4.5 Address Constant Expressions), he uses a code example where the address of a local variable is set to a constexpr variable. I thought this looked odd, so I tried running the example with g++ version 7.3.0 and was unable to get the same results. Here is his code example verbatim (although slightly abridged):



extern char glob;

void f(char loc)
constexpr const char* p0 = &glob; // OK: &glob's is a constant
constexpr const char* p2 = &loc; // OK: &loc is constant in its scope



When I run this, I get:



error: ‘(const char*)(& loc)’ is not a constant expression


Is something happening with g++ that I'm not aware of, or is there something more to Bjarne's example?










share|improve this question



















  • 2





    Clearly, &loc can't be a constexpr. However, these lines of code don't appear on my kindle version. He does show a constexpr for the address of a "C" style string in a local function. That's legal since these are in global space while loc is an argument on the stack and not constant. Is that example what you are referring to?

    – doug
    3 hours ago







  • 2





    char loc is a locally declared character that is not static. The next time f() is called there is no guarantee loc will have the same address. 10.4.5 makes that distinction between an address assigned by the linker and those assigned by the compiler. 2013 Stroustrup - The C++ Programming Language 4th Edition.pdf

    – David C. Rankin
    3 hours ago







  • 1





    @DavidC.Rankin it looks like your version (a pdf) is different from mine (a hard-copy). This must have been a mistake and was updated by the time your pdf was created.

    – johnnyodonnell
    3 hours ago






  • 2





    Yes, I was kinda scratching my head as to where &glob came from, but &loc was identifiable.

    – David C. Rankin
    3 hours ago






  • 1





    @DavidC.Rankin Good point, I should show that in my question. But yeah, I think this is because our versions are different

    – johnnyodonnell
    3 hours ago

















10















In Bjarne Stroustrup's book "The C++ Programming Language (4th Edition)" on p. 267 (Section 10.4.5 Address Constant Expressions), he uses a code example where the address of a local variable is set to a constexpr variable. I thought this looked odd, so I tried running the example with g++ version 7.3.0 and was unable to get the same results. Here is his code example verbatim (although slightly abridged):



extern char glob;

void f(char loc)
constexpr const char* p0 = &glob; // OK: &glob's is a constant
constexpr const char* p2 = &loc; // OK: &loc is constant in its scope



When I run this, I get:



error: ‘(const char*)(& loc)’ is not a constant expression


Is something happening with g++ that I'm not aware of, or is there something more to Bjarne's example?










share|improve this question



















  • 2





    Clearly, &loc can't be a constexpr. However, these lines of code don't appear on my kindle version. He does show a constexpr for the address of a "C" style string in a local function. That's legal since these are in global space while loc is an argument on the stack and not constant. Is that example what you are referring to?

    – doug
    3 hours ago







  • 2





    char loc is a locally declared character that is not static. The next time f() is called there is no guarantee loc will have the same address. 10.4.5 makes that distinction between an address assigned by the linker and those assigned by the compiler. 2013 Stroustrup - The C++ Programming Language 4th Edition.pdf

    – David C. Rankin
    3 hours ago







  • 1





    @DavidC.Rankin it looks like your version (a pdf) is different from mine (a hard-copy). This must have been a mistake and was updated by the time your pdf was created.

    – johnnyodonnell
    3 hours ago






  • 2





    Yes, I was kinda scratching my head as to where &glob came from, but &loc was identifiable.

    – David C. Rankin
    3 hours ago






  • 1





    @DavidC.Rankin Good point, I should show that in my question. But yeah, I think this is because our versions are different

    – johnnyodonnell
    3 hours ago













10












10








10


1






In Bjarne Stroustrup's book "The C++ Programming Language (4th Edition)" on p. 267 (Section 10.4.5 Address Constant Expressions), he uses a code example where the address of a local variable is set to a constexpr variable. I thought this looked odd, so I tried running the example with g++ version 7.3.0 and was unable to get the same results. Here is his code example verbatim (although slightly abridged):



extern char glob;

void f(char loc)
constexpr const char* p0 = &glob; // OK: &glob's is a constant
constexpr const char* p2 = &loc; // OK: &loc is constant in its scope



When I run this, I get:



error: ‘(const char*)(& loc)’ is not a constant expression


Is something happening with g++ that I'm not aware of, or is there something more to Bjarne's example?










share|improve this question
















In Bjarne Stroustrup's book "The C++ Programming Language (4th Edition)" on p. 267 (Section 10.4.5 Address Constant Expressions), he uses a code example where the address of a local variable is set to a constexpr variable. I thought this looked odd, so I tried running the example with g++ version 7.3.0 and was unable to get the same results. Here is his code example verbatim (although slightly abridged):



extern char glob;

void f(char loc)
constexpr const char* p0 = &glob; // OK: &glob's is a constant
constexpr const char* p2 = &loc; // OK: &loc is constant in its scope



When I run this, I get:



error: ‘(const char*)(& loc)’ is not a constant expression


Is something happening with g++ that I'm not aware of, or is there something more to Bjarne's example?







c++ c++11 constexpr memory-address






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 3 hours ago







johnnyodonnell

















asked 3 hours ago









johnnyodonnelljohnnyodonnell

398116




398116







  • 2





    Clearly, &loc can't be a constexpr. However, these lines of code don't appear on my kindle version. He does show a constexpr for the address of a "C" style string in a local function. That's legal since these are in global space while loc is an argument on the stack and not constant. Is that example what you are referring to?

    – doug
    3 hours ago







  • 2





    char loc is a locally declared character that is not static. The next time f() is called there is no guarantee loc will have the same address. 10.4.5 makes that distinction between an address assigned by the linker and those assigned by the compiler. 2013 Stroustrup - The C++ Programming Language 4th Edition.pdf

    – David C. Rankin
    3 hours ago







  • 1





    @DavidC.Rankin it looks like your version (a pdf) is different from mine (a hard-copy). This must have been a mistake and was updated by the time your pdf was created.

    – johnnyodonnell
    3 hours ago






  • 2





    Yes, I was kinda scratching my head as to where &glob came from, but &loc was identifiable.

    – David C. Rankin
    3 hours ago






  • 1





    @DavidC.Rankin Good point, I should show that in my question. But yeah, I think this is because our versions are different

    – johnnyodonnell
    3 hours ago












  • 2





    Clearly, &loc can't be a constexpr. However, these lines of code don't appear on my kindle version. He does show a constexpr for the address of a "C" style string in a local function. That's legal since these are in global space while loc is an argument on the stack and not constant. Is that example what you are referring to?

    – doug
    3 hours ago







  • 2





    char loc is a locally declared character that is not static. The next time f() is called there is no guarantee loc will have the same address. 10.4.5 makes that distinction between an address assigned by the linker and those assigned by the compiler. 2013 Stroustrup - The C++ Programming Language 4th Edition.pdf

    – David C. Rankin
    3 hours ago







  • 1





    @DavidC.Rankin it looks like your version (a pdf) is different from mine (a hard-copy). This must have been a mistake and was updated by the time your pdf was created.

    – johnnyodonnell
    3 hours ago






  • 2





    Yes, I was kinda scratching my head as to where &glob came from, but &loc was identifiable.

    – David C. Rankin
    3 hours ago






  • 1





    @DavidC.Rankin Good point, I should show that in my question. But yeah, I think this is because our versions are different

    – johnnyodonnell
    3 hours ago







2




2





Clearly, &loc can't be a constexpr. However, these lines of code don't appear on my kindle version. He does show a constexpr for the address of a "C" style string in a local function. That's legal since these are in global space while loc is an argument on the stack and not constant. Is that example what you are referring to?

– doug
3 hours ago






Clearly, &loc can't be a constexpr. However, these lines of code don't appear on my kindle version. He does show a constexpr for the address of a "C" style string in a local function. That's legal since these are in global space while loc is an argument on the stack and not constant. Is that example what you are referring to?

– doug
3 hours ago





2




2





char loc is a locally declared character that is not static. The next time f() is called there is no guarantee loc will have the same address. 10.4.5 makes that distinction between an address assigned by the linker and those assigned by the compiler. 2013 Stroustrup - The C++ Programming Language 4th Edition.pdf

– David C. Rankin
3 hours ago






char loc is a locally declared character that is not static. The next time f() is called there is no guarantee loc will have the same address. 10.4.5 makes that distinction between an address assigned by the linker and those assigned by the compiler. 2013 Stroustrup - The C++ Programming Language 4th Edition.pdf

– David C. Rankin
3 hours ago





1




1





@DavidC.Rankin it looks like your version (a pdf) is different from mine (a hard-copy). This must have been a mistake and was updated by the time your pdf was created.

– johnnyodonnell
3 hours ago





@DavidC.Rankin it looks like your version (a pdf) is different from mine (a hard-copy). This must have been a mistake and was updated by the time your pdf was created.

– johnnyodonnell
3 hours ago




2




2





Yes, I was kinda scratching my head as to where &glob came from, but &loc was identifiable.

– David C. Rankin
3 hours ago





Yes, I was kinda scratching my head as to where &glob came from, but &loc was identifiable.

– David C. Rankin
3 hours ago




1




1





@DavidC.Rankin Good point, I should show that in my question. But yeah, I think this is because our versions are different

– johnnyodonnell
3 hours ago





@DavidC.Rankin Good point, I should show that in my question. But yeah, I think this is because our versions are different

– johnnyodonnell
3 hours ago












3 Answers
3






active

oldest

votes


















7














Bjarne Stroustrup's book "The C++ Programming Language (4th Edition)" on p. 267 refers to the following code:



constexpr const char* p1="asdf";


This is OK because "asdf" is stored in a fixed memory location.



void f(char loc) 
constexpr const char* p0 = &glob; // OK: &glob's is a constant
constexpr const char* p2 = &loc; // OK: &loc is constant in its scope



However, loc is not in a fixed memory location. it's on the stack and will have varying locations depending on when it is called.






share|improve this answer























  • So, you're saying that Bjarne should not have said that &loc will be "OK", right?

    – johnnyodonnell
    3 hours ago












  • The example code I used in the question is taken verbatim. After looking at @doug's pdf, I think the hard-copy book that I own is incorrect. I think this mistake was updated in later versions.

    – johnnyodonnell
    3 hours ago











  • Is it the same as this? github.com/boydfd/books/blob/master/seeing/stalled/…

    – jackw11111
    3 hours ago











  • @jackw11111 my version is different from the pdf provided in that link. doug provided a link to the same pdf. My hard-copy and that pdf show different examples for section 10.4.5

    – johnnyodonnell
    3 hours ago


















3














It appears that the example from section 10.4.5 provided in my hard-copy of the "The C++ Programming Language (4th Edition)" is incorrect. And so I've concluded that the address of a local variable is not a constexpr.



The example appears to have been updated in some pdf versions as seen here:



enter image description here






share|improve this answer






























    1














    Just to add to other answers that have pointed out the mistake, C++ standard only allows constexpr pointers to objects of static-storage duration, one past the end of such, or nullptr. See [expr.const/8] specifically #8.2;



    It's worth noting that:




    • string-literals have static-storage duration:

    • Based on constraints in declaring extern variables, they'll inherently have static-storage duration or thread local-storage duration.

    Hence this is valid:



    #include <string>

    extern char glob;
    std::string boom = "Haha";

    void f(char loc)
    constexpr const char* p1 = &glob;
    constexpr std::string* p2 = nullptr;
    constexpr std::string* p3 = &boom;






    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%2f55698844%2fis-the-address-of-a-local-variable-a-constexpr%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      7














      Bjarne Stroustrup's book "The C++ Programming Language (4th Edition)" on p. 267 refers to the following code:



      constexpr const char* p1="asdf";


      This is OK because "asdf" is stored in a fixed memory location.



      void f(char loc) 
      constexpr const char* p0 = &glob; // OK: &glob's is a constant
      constexpr const char* p2 = &loc; // OK: &loc is constant in its scope



      However, loc is not in a fixed memory location. it's on the stack and will have varying locations depending on when it is called.






      share|improve this answer























      • So, you're saying that Bjarne should not have said that &loc will be "OK", right?

        – johnnyodonnell
        3 hours ago












      • The example code I used in the question is taken verbatim. After looking at @doug's pdf, I think the hard-copy book that I own is incorrect. I think this mistake was updated in later versions.

        – johnnyodonnell
        3 hours ago











      • Is it the same as this? github.com/boydfd/books/blob/master/seeing/stalled/…

        – jackw11111
        3 hours ago











      • @jackw11111 my version is different from the pdf provided in that link. doug provided a link to the same pdf. My hard-copy and that pdf show different examples for section 10.4.5

        – johnnyodonnell
        3 hours ago















      7














      Bjarne Stroustrup's book "The C++ Programming Language (4th Edition)" on p. 267 refers to the following code:



      constexpr const char* p1="asdf";


      This is OK because "asdf" is stored in a fixed memory location.



      void f(char loc) 
      constexpr const char* p0 = &glob; // OK: &glob's is a constant
      constexpr const char* p2 = &loc; // OK: &loc is constant in its scope



      However, loc is not in a fixed memory location. it's on the stack and will have varying locations depending on when it is called.






      share|improve this answer























      • So, you're saying that Bjarne should not have said that &loc will be "OK", right?

        – johnnyodonnell
        3 hours ago












      • The example code I used in the question is taken verbatim. After looking at @doug's pdf, I think the hard-copy book that I own is incorrect. I think this mistake was updated in later versions.

        – johnnyodonnell
        3 hours ago











      • Is it the same as this? github.com/boydfd/books/blob/master/seeing/stalled/…

        – jackw11111
        3 hours ago











      • @jackw11111 my version is different from the pdf provided in that link. doug provided a link to the same pdf. My hard-copy and that pdf show different examples for section 10.4.5

        – johnnyodonnell
        3 hours ago













      7












      7








      7







      Bjarne Stroustrup's book "The C++ Programming Language (4th Edition)" on p. 267 refers to the following code:



      constexpr const char* p1="asdf";


      This is OK because "asdf" is stored in a fixed memory location.



      void f(char loc) 
      constexpr const char* p0 = &glob; // OK: &glob's is a constant
      constexpr const char* p2 = &loc; // OK: &loc is constant in its scope



      However, loc is not in a fixed memory location. it's on the stack and will have varying locations depending on when it is called.






      share|improve this answer













      Bjarne Stroustrup's book "The C++ Programming Language (4th Edition)" on p. 267 refers to the following code:



      constexpr const char* p1="asdf";


      This is OK because "asdf" is stored in a fixed memory location.



      void f(char loc) 
      constexpr const char* p0 = &glob; // OK: &glob's is a constant
      constexpr const char* p2 = &loc; // OK: &loc is constant in its scope



      However, loc is not in a fixed memory location. it's on the stack and will have varying locations depending on when it is called.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 3 hours ago









      dougdoug

      8721410




      8721410












      • So, you're saying that Bjarne should not have said that &loc will be "OK", right?

        – johnnyodonnell
        3 hours ago












      • The example code I used in the question is taken verbatim. After looking at @doug's pdf, I think the hard-copy book that I own is incorrect. I think this mistake was updated in later versions.

        – johnnyodonnell
        3 hours ago











      • Is it the same as this? github.com/boydfd/books/blob/master/seeing/stalled/…

        – jackw11111
        3 hours ago











      • @jackw11111 my version is different from the pdf provided in that link. doug provided a link to the same pdf. My hard-copy and that pdf show different examples for section 10.4.5

        – johnnyodonnell
        3 hours ago

















      • So, you're saying that Bjarne should not have said that &loc will be "OK", right?

        – johnnyodonnell
        3 hours ago












      • The example code I used in the question is taken verbatim. After looking at @doug's pdf, I think the hard-copy book that I own is incorrect. I think this mistake was updated in later versions.

        – johnnyodonnell
        3 hours ago











      • Is it the same as this? github.com/boydfd/books/blob/master/seeing/stalled/…

        – jackw11111
        3 hours ago











      • @jackw11111 my version is different from the pdf provided in that link. doug provided a link to the same pdf. My hard-copy and that pdf show different examples for section 10.4.5

        – johnnyodonnell
        3 hours ago
















      So, you're saying that Bjarne should not have said that &loc will be "OK", right?

      – johnnyodonnell
      3 hours ago






      So, you're saying that Bjarne should not have said that &loc will be "OK", right?

      – johnnyodonnell
      3 hours ago














      The example code I used in the question is taken verbatim. After looking at @doug's pdf, I think the hard-copy book that I own is incorrect. I think this mistake was updated in later versions.

      – johnnyodonnell
      3 hours ago





      The example code I used in the question is taken verbatim. After looking at @doug's pdf, I think the hard-copy book that I own is incorrect. I think this mistake was updated in later versions.

      – johnnyodonnell
      3 hours ago













      Is it the same as this? github.com/boydfd/books/blob/master/seeing/stalled/…

      – jackw11111
      3 hours ago





      Is it the same as this? github.com/boydfd/books/blob/master/seeing/stalled/…

      – jackw11111
      3 hours ago













      @jackw11111 my version is different from the pdf provided in that link. doug provided a link to the same pdf. My hard-copy and that pdf show different examples for section 10.4.5

      – johnnyodonnell
      3 hours ago





      @jackw11111 my version is different from the pdf provided in that link. doug provided a link to the same pdf. My hard-copy and that pdf show different examples for section 10.4.5

      – johnnyodonnell
      3 hours ago













      3














      It appears that the example from section 10.4.5 provided in my hard-copy of the "The C++ Programming Language (4th Edition)" is incorrect. And so I've concluded that the address of a local variable is not a constexpr.



      The example appears to have been updated in some pdf versions as seen here:



      enter image description here






      share|improve this answer



























        3














        It appears that the example from section 10.4.5 provided in my hard-copy of the "The C++ Programming Language (4th Edition)" is incorrect. And so I've concluded that the address of a local variable is not a constexpr.



        The example appears to have been updated in some pdf versions as seen here:



        enter image description here






        share|improve this answer

























          3












          3








          3







          It appears that the example from section 10.4.5 provided in my hard-copy of the "The C++ Programming Language (4th Edition)" is incorrect. And so I've concluded that the address of a local variable is not a constexpr.



          The example appears to have been updated in some pdf versions as seen here:



          enter image description here






          share|improve this answer













          It appears that the example from section 10.4.5 provided in my hard-copy of the "The C++ Programming Language (4th Edition)" is incorrect. And so I've concluded that the address of a local variable is not a constexpr.



          The example appears to have been updated in some pdf versions as seen here:



          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 3 hours ago









          johnnyodonnelljohnnyodonnell

          398116




          398116





















              1














              Just to add to other answers that have pointed out the mistake, C++ standard only allows constexpr pointers to objects of static-storage duration, one past the end of such, or nullptr. See [expr.const/8] specifically #8.2;



              It's worth noting that:




              • string-literals have static-storage duration:

              • Based on constraints in declaring extern variables, they'll inherently have static-storage duration or thread local-storage duration.

              Hence this is valid:



              #include <string>

              extern char glob;
              std::string boom = "Haha";

              void f(char loc)
              constexpr const char* p1 = &glob;
              constexpr std::string* p2 = nullptr;
              constexpr std::string* p3 = &boom;






              share|improve this answer



























                1














                Just to add to other answers that have pointed out the mistake, C++ standard only allows constexpr pointers to objects of static-storage duration, one past the end of such, or nullptr. See [expr.const/8] specifically #8.2;



                It's worth noting that:




                • string-literals have static-storage duration:

                • Based on constraints in declaring extern variables, they'll inherently have static-storage duration or thread local-storage duration.

                Hence this is valid:



                #include <string>

                extern char glob;
                std::string boom = "Haha";

                void f(char loc)
                constexpr const char* p1 = &glob;
                constexpr std::string* p2 = nullptr;
                constexpr std::string* p3 = &boom;






                share|improve this answer

























                  1












                  1








                  1







                  Just to add to other answers that have pointed out the mistake, C++ standard only allows constexpr pointers to objects of static-storage duration, one past the end of such, or nullptr. See [expr.const/8] specifically #8.2;



                  It's worth noting that:




                  • string-literals have static-storage duration:

                  • Based on constraints in declaring extern variables, they'll inherently have static-storage duration or thread local-storage duration.

                  Hence this is valid:



                  #include <string>

                  extern char glob;
                  std::string boom = "Haha";

                  void f(char loc)
                  constexpr const char* p1 = &glob;
                  constexpr std::string* p2 = nullptr;
                  constexpr std::string* p3 = &boom;






                  share|improve this answer













                  Just to add to other answers that have pointed out the mistake, C++ standard only allows constexpr pointers to objects of static-storage duration, one past the end of such, or nullptr. See [expr.const/8] specifically #8.2;



                  It's worth noting that:




                  • string-literals have static-storage duration:

                  • Based on constraints in declaring extern variables, they'll inherently have static-storage duration or thread local-storage duration.

                  Hence this is valid:



                  #include <string>

                  extern char glob;
                  std::string boom = "Haha";

                  void f(char loc)
                  constexpr const char* p1 = &glob;
                  constexpr std::string* p2 = nullptr;
                  constexpr std::string* p3 = &boom;







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 hours ago









                  WhiZTiMWhiZTiM

                  18.1k33153




                  18.1k33153



























                      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%2f55698844%2fis-the-address-of-a-local-variable-a-constexpr%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

                      Oświęcim Innehåll Historia | Källor | Externa länkar | Navigeringsmeny50°2′18″N 19°13′17″Ö / 50.03833°N 19.22139°Ö / 50.03833; 19.2213950°2′18″N 19°13′17″Ö / 50.03833°N 19.22139°Ö / 50.03833; 19.221393089658Nordisk familjebok, AuschwitzInsidan tro och existensJewish Community i OświęcimAuschwitz Jewish Center: MuseumAuschwitz Jewish Center

                      Valle di Casies Indice Geografia fisica | Origini del nome | Storia | Società | Amministrazione | Sport | Note | Bibliografia | Voci correlate | Altri progetti | Collegamenti esterni | Menu di navigazione46°46′N 12°11′E / 46.766667°N 12.183333°E46.766667; 12.183333 (Valle di Casies)46°46′N 12°11′E / 46.766667°N 12.183333°E46.766667; 12.183333 (Valle di Casies)Sito istituzionaleAstat Censimento della popolazione 2011 - Determinazione della consistenza dei tre gruppi linguistici della Provincia Autonoma di Bolzano-Alto Adige - giugno 2012Numeri e fattiValle di CasiesDato IstatTabella dei gradi/giorno dei Comuni italiani raggruppati per Regione e Provincia26 agosto 1993, n. 412Heraldry of the World: GsiesStatistiche I.StatValCasies.comWikimedia CommonsWikimedia CommonsValle di CasiesSito ufficialeValle di CasiesMM14870458910042978-6

                      Typsetting diagram chases (with TikZ?) Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)How to define the default vertical distance between nodes?Draw edge on arcNumerical conditional within tikz keys?TikZ: Drawing an arc from an intersection to an intersectionDrawing rectilinear curves in Tikz, aka an Etch-a-Sketch drawingLine up nested tikz enviroments or how to get rid of themHow to place nodes in an absolute coordinate system in tikzCommutative diagram with curve connecting between nodesTikz with standalone: pinning tikz coordinates to page cmDrawing a Decision Diagram with Tikz and layout manager