Templates and ODR












5














The question is about the C++ documentation and standard documentation. Is in the following code the variable x odr-used?



extern int x;
template<class T> T f() { return x; }


It seems to me it is not used, bud where it is stated in documents? (there is the statement, that appearance x in expression is odr-use, but...)










share|improve this question





























    5














    The question is about the C++ documentation and standard documentation. Is in the following code the variable x odr-used?



    extern int x;
    template<class T> T f() { return x; }


    It seems to me it is not used, bud where it is stated in documents? (there is the statement, that appearance x in expression is odr-use, but...)










    share|improve this question



























      5












      5








      5







      The question is about the C++ documentation and standard documentation. Is in the following code the variable x odr-used?



      extern int x;
      template<class T> T f() { return x; }


      It seems to me it is not used, bud where it is stated in documents? (there is the statement, that appearance x in expression is odr-use, but...)










      share|improve this question















      The question is about the C++ documentation and standard documentation. Is in the following code the variable x odr-used?



      extern int x;
      template<class T> T f() { return x; }


      It seems to me it is not used, bud where it is stated in documents? (there is the statement, that appearance x in expression is odr-use, but...)







      c++ templates language-lawyer






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 8:00









      StoryTeller

      93.7k12187253




      93.7k12187253










      asked Nov 20 '18 at 7:51









      guest

      542




      542
























          1 Answer
          1






          active

          oldest

          votes


















          1














          It is odr-used. [basic.def.odr]/2:




          An expression is potentially evaluated unless it is an unevaluated operand or a subexpression thereof.




          Thus the expression x is potentially evaluated.



          [basic.def.odr]/4:




          A variable x whose name appears as a potentially-evaluated expression ex is odr-used by ex unless applying the lvalue-to-rvalue conversion ([conv.lval]) to x yields a constant expression ([expr.const]) [...]




          We can stop here. Applying the lvalue-to-rvalue conversion to x does not yield a constant expression.



          This violation of the ODR does not require a diagnostic ([basic.def.odr]/10):




          Every program shall contain exactly one definition of every non-inline
          function or variable that is odr-used in that program outside of a
          discarded statement; no diagnostic required.






          Even assuming for the sake of argument that f does not odr-use x until/unless a specialization is generated, it still won't help the program, because the program is also ill-formed NDR if "no valid specialization can be generated for a template" ([temp.res]/8), and since every valid specialization of f undoubtedly odr-uses x, no valid specialization can be generated if a definition of x is not present in the program.






          share|improve this answer





















          • What then about instantiation of templates when they are used? If x were a template or static data member of a class template... It should be then instantiated. But I don't found compilers to do this...
            – guest
            Nov 20 '18 at 9:37













          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%2f53388442%2ftemplates-and-odr%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          It is odr-used. [basic.def.odr]/2:




          An expression is potentially evaluated unless it is an unevaluated operand or a subexpression thereof.




          Thus the expression x is potentially evaluated.



          [basic.def.odr]/4:




          A variable x whose name appears as a potentially-evaluated expression ex is odr-used by ex unless applying the lvalue-to-rvalue conversion ([conv.lval]) to x yields a constant expression ([expr.const]) [...]




          We can stop here. Applying the lvalue-to-rvalue conversion to x does not yield a constant expression.



          This violation of the ODR does not require a diagnostic ([basic.def.odr]/10):




          Every program shall contain exactly one definition of every non-inline
          function or variable that is odr-used in that program outside of a
          discarded statement; no diagnostic required.






          Even assuming for the sake of argument that f does not odr-use x until/unless a specialization is generated, it still won't help the program, because the program is also ill-formed NDR if "no valid specialization can be generated for a template" ([temp.res]/8), and since every valid specialization of f undoubtedly odr-uses x, no valid specialization can be generated if a definition of x is not present in the program.






          share|improve this answer





















          • What then about instantiation of templates when they are used? If x were a template or static data member of a class template... It should be then instantiated. But I don't found compilers to do this...
            – guest
            Nov 20 '18 at 9:37


















          1














          It is odr-used. [basic.def.odr]/2:




          An expression is potentially evaluated unless it is an unevaluated operand or a subexpression thereof.




          Thus the expression x is potentially evaluated.



          [basic.def.odr]/4:




          A variable x whose name appears as a potentially-evaluated expression ex is odr-used by ex unless applying the lvalue-to-rvalue conversion ([conv.lval]) to x yields a constant expression ([expr.const]) [...]




          We can stop here. Applying the lvalue-to-rvalue conversion to x does not yield a constant expression.



          This violation of the ODR does not require a diagnostic ([basic.def.odr]/10):




          Every program shall contain exactly one definition of every non-inline
          function or variable that is odr-used in that program outside of a
          discarded statement; no diagnostic required.






          Even assuming for the sake of argument that f does not odr-use x until/unless a specialization is generated, it still won't help the program, because the program is also ill-formed NDR if "no valid specialization can be generated for a template" ([temp.res]/8), and since every valid specialization of f undoubtedly odr-uses x, no valid specialization can be generated if a definition of x is not present in the program.






          share|improve this answer





















          • What then about instantiation of templates when they are used? If x were a template or static data member of a class template... It should be then instantiated. But I don't found compilers to do this...
            – guest
            Nov 20 '18 at 9:37
















          1












          1








          1






          It is odr-used. [basic.def.odr]/2:




          An expression is potentially evaluated unless it is an unevaluated operand or a subexpression thereof.




          Thus the expression x is potentially evaluated.



          [basic.def.odr]/4:




          A variable x whose name appears as a potentially-evaluated expression ex is odr-used by ex unless applying the lvalue-to-rvalue conversion ([conv.lval]) to x yields a constant expression ([expr.const]) [...]




          We can stop here. Applying the lvalue-to-rvalue conversion to x does not yield a constant expression.



          This violation of the ODR does not require a diagnostic ([basic.def.odr]/10):




          Every program shall contain exactly one definition of every non-inline
          function or variable that is odr-used in that program outside of a
          discarded statement; no diagnostic required.






          Even assuming for the sake of argument that f does not odr-use x until/unless a specialization is generated, it still won't help the program, because the program is also ill-formed NDR if "no valid specialization can be generated for a template" ([temp.res]/8), and since every valid specialization of f undoubtedly odr-uses x, no valid specialization can be generated if a definition of x is not present in the program.






          share|improve this answer












          It is odr-used. [basic.def.odr]/2:




          An expression is potentially evaluated unless it is an unevaluated operand or a subexpression thereof.




          Thus the expression x is potentially evaluated.



          [basic.def.odr]/4:




          A variable x whose name appears as a potentially-evaluated expression ex is odr-used by ex unless applying the lvalue-to-rvalue conversion ([conv.lval]) to x yields a constant expression ([expr.const]) [...]




          We can stop here. Applying the lvalue-to-rvalue conversion to x does not yield a constant expression.



          This violation of the ODR does not require a diagnostic ([basic.def.odr]/10):




          Every program shall contain exactly one definition of every non-inline
          function or variable that is odr-used in that program outside of a
          discarded statement; no diagnostic required.






          Even assuming for the sake of argument that f does not odr-use x until/unless a specialization is generated, it still won't help the program, because the program is also ill-formed NDR if "no valid specialization can be generated for a template" ([temp.res]/8), and since every valid specialization of f undoubtedly odr-uses x, no valid specialization can be generated if a definition of x is not present in the program.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 8:50









          T.C.

          106k13216321




          106k13216321












          • What then about instantiation of templates when they are used? If x were a template or static data member of a class template... It should be then instantiated. But I don't found compilers to do this...
            – guest
            Nov 20 '18 at 9:37




















          • What then about instantiation of templates when they are used? If x were a template or static data member of a class template... It should be then instantiated. But I don't found compilers to do this...
            – guest
            Nov 20 '18 at 9:37


















          What then about instantiation of templates when they are used? If x were a template or static data member of a class template... It should be then instantiated. But I don't found compilers to do this...
          – guest
          Nov 20 '18 at 9:37






          What then about instantiation of templates when they are used? If x were a template or static data member of a class template... It should be then instantiated. But I don't found compilers to do this...
          – guest
          Nov 20 '18 at 9:37




















          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%2f53388442%2ftemplates-and-odr%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

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy