Instantiating template function in a false if constexpr gives error











up vote
2
down vote

favorite
1












Consider the following program:



#include <iostream>

template<typename... Params_t>
constexpr int constexprValue(Params_t...) { return 5; }

int main()
{
const bool flag = true;

if constexpr(flag)
{
constexpr int value = constexprValue(1, 2, 3);
std::cout << value << "n";
}
}


This compiles and works fine. However, if flag is changed to false, then clang (Apple LLVM version 10.0.0 (clang-1000.10.44.4)) gives a compiler error:



error: constexpr variable 'value' must be initialized by a constant expression
undefined function 'constexprValue<int, int, int>' cannot be used in a constant expression


Is this a bug in clang?










share|improve this question




























    up vote
    2
    down vote

    favorite
    1












    Consider the following program:



    #include <iostream>

    template<typename... Params_t>
    constexpr int constexprValue(Params_t...) { return 5; }

    int main()
    {
    const bool flag = true;

    if constexpr(flag)
    {
    constexpr int value = constexprValue(1, 2, 3);
    std::cout << value << "n";
    }
    }


    This compiles and works fine. However, if flag is changed to false, then clang (Apple LLVM version 10.0.0 (clang-1000.10.44.4)) gives a compiler error:



    error: constexpr variable 'value' must be initialized by a constant expression
    undefined function 'constexprValue<int, int, int>' cannot be used in a constant expression


    Is this a bug in clang?










    share|improve this question


























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      Consider the following program:



      #include <iostream>

      template<typename... Params_t>
      constexpr int constexprValue(Params_t...) { return 5; }

      int main()
      {
      const bool flag = true;

      if constexpr(flag)
      {
      constexpr int value = constexprValue(1, 2, 3);
      std::cout << value << "n";
      }
      }


      This compiles and works fine. However, if flag is changed to false, then clang (Apple LLVM version 10.0.0 (clang-1000.10.44.4)) gives a compiler error:



      error: constexpr variable 'value' must be initialized by a constant expression
      undefined function 'constexprValue<int, int, int>' cannot be used in a constant expression


      Is this a bug in clang?










      share|improve this question















      Consider the following program:



      #include <iostream>

      template<typename... Params_t>
      constexpr int constexprValue(Params_t...) { return 5; }

      int main()
      {
      const bool flag = true;

      if constexpr(flag)
      {
      constexpr int value = constexprValue(1, 2, 3);
      std::cout << value << "n";
      }
      }


      This compiles and works fine. However, if flag is changed to false, then clang (Apple LLVM version 10.0.0 (clang-1000.10.44.4)) gives a compiler error:



      error: constexpr variable 'value' must be initialized by a constant expression
      undefined function 'constexprValue<int, int, int>' cannot be used in a constant expression


      Is this a bug in clang?







      c++ c++17 clang++ if-constexpr






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 at 16:53









      Shafik Yaghmour

      123k23306509




      123k23306509










      asked Nov 7 at 10:22









      Warp

      955




      955
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Yes this is a bug it was fixed by this commmit to clang: [Sema] Discarded statment should be an evaluatable context. which has the following description:




          The constexpr evaluator was erroring out because these templates weren't
          defined. Despite being used in a discarded statement, we still need to constexpr
          evaluate them, which means that we need to instantiate them. Fixes PR37585.



          Differential revision: https://reviews.llvm.org/D48322




          and includes the following test:



          namespace PR37585 {
          template <class T> struct S { static constexpr bool value = true; };
          template <class T> constexpr bool f() { return true; }
          template <class T> constexpr bool v = true;

          void test() {
          if constexpr (true) {}
          else if constexpr (f<int>()) {}
          else if constexpr (S<int>::value) {}
          else if constexpr (v<int>) {}
          }
          }


          If we try the test live with godbolt with an older clang version we obtain a very similar erroneous diagnostic that your example is seeing:



          error: constexpr if condition is not a constant expression
          else if constexpr (f<int>()) {}
          ^~~~~~~~

          note: undefined function 'f<int>' cannot be used in a constant expression


          the fix originated from bug report: constexpr if condition is not a constant expression and std::is_same.






          share|improve this answer






























            up vote
            1
            down vote













            Yes, it seems to be a bug in the Apple version of clang, I'm able to compile the code with clang version 5 and greater as well as gcc version 7.1 and greater.



            Matt Godbold has a great website for compiling snippets of code with a slew of different compilers.



            Here is a link to your example in godbolt.






            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: 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%2f53187528%2finstantiating-template-function-in-a-false-if-constexpr-gives-error%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
              1
              down vote



              accepted










              Yes this is a bug it was fixed by this commmit to clang: [Sema] Discarded statment should be an evaluatable context. which has the following description:




              The constexpr evaluator was erroring out because these templates weren't
              defined. Despite being used in a discarded statement, we still need to constexpr
              evaluate them, which means that we need to instantiate them. Fixes PR37585.



              Differential revision: https://reviews.llvm.org/D48322




              and includes the following test:



              namespace PR37585 {
              template <class T> struct S { static constexpr bool value = true; };
              template <class T> constexpr bool f() { return true; }
              template <class T> constexpr bool v = true;

              void test() {
              if constexpr (true) {}
              else if constexpr (f<int>()) {}
              else if constexpr (S<int>::value) {}
              else if constexpr (v<int>) {}
              }
              }


              If we try the test live with godbolt with an older clang version we obtain a very similar erroneous diagnostic that your example is seeing:



              error: constexpr if condition is not a constant expression
              else if constexpr (f<int>()) {}
              ^~~~~~~~

              note: undefined function 'f<int>' cannot be used in a constant expression


              the fix originated from bug report: constexpr if condition is not a constant expression and std::is_same.






              share|improve this answer



























                up vote
                1
                down vote



                accepted










                Yes this is a bug it was fixed by this commmit to clang: [Sema] Discarded statment should be an evaluatable context. which has the following description:




                The constexpr evaluator was erroring out because these templates weren't
                defined. Despite being used in a discarded statement, we still need to constexpr
                evaluate them, which means that we need to instantiate them. Fixes PR37585.



                Differential revision: https://reviews.llvm.org/D48322




                and includes the following test:



                namespace PR37585 {
                template <class T> struct S { static constexpr bool value = true; };
                template <class T> constexpr bool f() { return true; }
                template <class T> constexpr bool v = true;

                void test() {
                if constexpr (true) {}
                else if constexpr (f<int>()) {}
                else if constexpr (S<int>::value) {}
                else if constexpr (v<int>) {}
                }
                }


                If we try the test live with godbolt with an older clang version we obtain a very similar erroneous diagnostic that your example is seeing:



                error: constexpr if condition is not a constant expression
                else if constexpr (f<int>()) {}
                ^~~~~~~~

                note: undefined function 'f<int>' cannot be used in a constant expression


                the fix originated from bug report: constexpr if condition is not a constant expression and std::is_same.






                share|improve this answer

























                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  Yes this is a bug it was fixed by this commmit to clang: [Sema] Discarded statment should be an evaluatable context. which has the following description:




                  The constexpr evaluator was erroring out because these templates weren't
                  defined. Despite being used in a discarded statement, we still need to constexpr
                  evaluate them, which means that we need to instantiate them. Fixes PR37585.



                  Differential revision: https://reviews.llvm.org/D48322




                  and includes the following test:



                  namespace PR37585 {
                  template <class T> struct S { static constexpr bool value = true; };
                  template <class T> constexpr bool f() { return true; }
                  template <class T> constexpr bool v = true;

                  void test() {
                  if constexpr (true) {}
                  else if constexpr (f<int>()) {}
                  else if constexpr (S<int>::value) {}
                  else if constexpr (v<int>) {}
                  }
                  }


                  If we try the test live with godbolt with an older clang version we obtain a very similar erroneous diagnostic that your example is seeing:



                  error: constexpr if condition is not a constant expression
                  else if constexpr (f<int>()) {}
                  ^~~~~~~~

                  note: undefined function 'f<int>' cannot be used in a constant expression


                  the fix originated from bug report: constexpr if condition is not a constant expression and std::is_same.






                  share|improve this answer














                  Yes this is a bug it was fixed by this commmit to clang: [Sema] Discarded statment should be an evaluatable context. which has the following description:




                  The constexpr evaluator was erroring out because these templates weren't
                  defined. Despite being used in a discarded statement, we still need to constexpr
                  evaluate them, which means that we need to instantiate them. Fixes PR37585.



                  Differential revision: https://reviews.llvm.org/D48322




                  and includes the following test:



                  namespace PR37585 {
                  template <class T> struct S { static constexpr bool value = true; };
                  template <class T> constexpr bool f() { return true; }
                  template <class T> constexpr bool v = true;

                  void test() {
                  if constexpr (true) {}
                  else if constexpr (f<int>()) {}
                  else if constexpr (S<int>::value) {}
                  else if constexpr (v<int>) {}
                  }
                  }


                  If we try the test live with godbolt with an older clang version we obtain a very similar erroneous diagnostic that your example is seeing:



                  error: constexpr if condition is not a constant expression
                  else if constexpr (f<int>()) {}
                  ^~~~~~~~

                  note: undefined function 'f<int>' cannot be used in a constant expression


                  the fix originated from bug report: constexpr if condition is not a constant expression and std::is_same.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 8 at 17:00

























                  answered Nov 7 at 16:52









                  Shafik Yaghmour

                  123k23306509




                  123k23306509
























                      up vote
                      1
                      down vote













                      Yes, it seems to be a bug in the Apple version of clang, I'm able to compile the code with clang version 5 and greater as well as gcc version 7.1 and greater.



                      Matt Godbold has a great website for compiling snippets of code with a slew of different compilers.



                      Here is a link to your example in godbolt.






                      share|improve this answer

























                        up vote
                        1
                        down vote













                        Yes, it seems to be a bug in the Apple version of clang, I'm able to compile the code with clang version 5 and greater as well as gcc version 7.1 and greater.



                        Matt Godbold has a great website for compiling snippets of code with a slew of different compilers.



                        Here is a link to your example in godbolt.






                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          Yes, it seems to be a bug in the Apple version of clang, I'm able to compile the code with clang version 5 and greater as well as gcc version 7.1 and greater.



                          Matt Godbold has a great website for compiling snippets of code with a slew of different compilers.



                          Here is a link to your example in godbolt.






                          share|improve this answer












                          Yes, it seems to be a bug in the Apple version of clang, I'm able to compile the code with clang version 5 and greater as well as gcc version 7.1 and greater.



                          Matt Godbold has a great website for compiling snippets of code with a slew of different compilers.



                          Here is a link to your example in godbolt.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 7 at 10:37









                          MaLarsson

                          10526




                          10526






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53187528%2finstantiating-template-function-in-a-false-if-constexpr-gives-error%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







                              這個網誌中的熱門文章

                              Xamarin.form Move up view when keyboard appear

                              Post-Redirect-Get with Spring WebFlux and Thymeleaf

                              Anylogic : not able to use stopDelay()