If there's an if-constexpr, how come there's no switch-constexpr?












4















In C++17, if constexpr was introduced; however, there doesn't seem to be a switch constexpr (see here). Why is that? That is, if a compiler supports if constexpr, is it not also trivial for it to support switch constexpr (at worst as an if-then-else-if-etc. chain, or multiple if's with some flags to control fallthrough)?










share|improve this question





























    4















    In C++17, if constexpr was introduced; however, there doesn't seem to be a switch constexpr (see here). Why is that? That is, if a compiler supports if constexpr, is it not also trivial for it to support switch constexpr (at worst as an if-then-else-if-etc. chain, or multiple if's with some flags to control fallthrough)?










    share|improve this question



























      4












      4








      4








      In C++17, if constexpr was introduced; however, there doesn't seem to be a switch constexpr (see here). Why is that? That is, if a compiler supports if constexpr, is it not also trivial for it to support switch constexpr (at worst as an if-then-else-if-etc. chain, or multiple if's with some flags to control fallthrough)?










      share|improve this question
















      In C++17, if constexpr was introduced; however, there doesn't seem to be a switch constexpr (see here). Why is that? That is, if a compiler supports if constexpr, is it not also trivial for it to support switch constexpr (at worst as an if-then-else-if-etc. chain, or multiple if's with some flags to control fallthrough)?







      c++ c++17 c++20 if-constexpr switch-constexpr






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 19:42









      T.C.

      107k14219323




      107k14219323










      asked Nov 19 '18 at 16:42









      einpoklumeinpoklum

      34.6k27128246




      34.6k27128246
























          3 Answers
          3






          active

          oldest

          votes


















          13














          if constexpr was ultimately derived from a more sane form of the static if concept. Because of that derivation, applying the same idea to switch does not appear to have been considered by the standards committee. So this is likely the primary reason: nobody added it to the paper since it was a restricted form of a syntax where switch wouldn't have made sense.



          That being said, switch has a lot of baggage in it. The most notable bit being the automatic fallthrough behavior. That makes defining its behavior a bit problematic.



          See, one of the powers of if constexpr is to make the side not taken at compile time be discarded under certain conditions. This is an important part of the syntax. So a hypothetical switch constexpr would be expected to have similar powers.



          That's a lot harder to do with fallthrough, since the case blocks are not as fundamentally distinct as the two blocks of an if statement. Especially if you have conditional fallthrough. Now, you could make switch constexpr not have automatic fallthrough (or fallthrough at all), so that the different sections are distinct. But then you've subtly changed how the syntax works; a non-constexpr form of switch behaves differently from the constexpr form. That's not good.



          Yes, you could make it a compile error to not put break; statements between the labels.



          Note that the two main pattern-matching proposals, P1308 and P1260, specifically avoid using switch, instead inventing a new keyword. They both have constexpr aspects to them, but they make it abundantly clear that they are not switch/case.






          share|improve this answer



















          • 4





            While both proposals are interesting, P1308 introduces syntax that looks very little like current C++. P2160 is quite a fascinating paper.

            – Jon Harper
            Nov 19 '18 at 20:47






          • 3





            @JonHarper: "P1308 introduces syntax that looks very little like current C++." You know, normally I would have responded with "you say that as if it's a bad thing ;)". But after looking at that proposal, it's kinda scary in there.

            – Nicol Bolas
            Nov 19 '18 at 21:15






          • 1





            Excellent explanation and references. Haskell++, anyone?

            – cantordust
            Nov 20 '18 at 2:15






          • 1





            @NicolBolas [.scary true]

            – Yakk - Adam Nevraumont
            Nov 23 '18 at 19:08



















          0














          I am not authoritative, but if you look at the selection statements if has a clear separation between true and false statements, switch doesn't.



          I presume it would be relatively harder to discard unused parts of switch from evaluation, especially since they can fall-through.



          Reimplementing switch as if-else-if is not so straight forward if you want to maintain all the (exotic) capabilities of it.






          share|improve this answer
























          • I'm sure there is a simple way to transform any switch into a tree of if branches, even considering fallthrough.

            – Nelfeal
            Nov 19 '18 at 17:15











          • See slight edit regarding the complexity of if

            – einpoklum
            Nov 19 '18 at 17:17






          • 1





            See Duff's device. switch is a lot less structured than most people think.

            – T.C.
            Nov 19 '18 at 19:41













          • @T.C. I am not sure who what were you commenting on, my answer or the 1st comment, but that is exactly my point. Because of such lack of the structure it would be pretty hard to reimplement it. It's way easier to base discarding rules on whole statement - using existing syntax, than coming up with custom rules for something amorphous.

            – luk32
            Nov 19 '18 at 22:04





















          0














          Consider the following example from the answer to another question (about optimized if branches).



          struct Cat { void meow() { } };
          struct Dog { void bark() { } };

          template <typename T>
          void pet(T x)
          {
          if(std::is_same<T, Cat>{}){ x.meow(); }
          else if(std::is_same<T, Dog>{}){ x.bark(); }
          }

          pet(Cat{});
          pet(Dog{});


          You cannot replicate this with a switch because you would need the case values to actually be types; something like the following, which is impossible for obvious reasons.



          template <typename T>
          void pet(T x)
          {
          switch (T) {
          case Cat:
          x.meow();
          break;
          case Dog:
          x.meow();
          break;
          }
          }


          This example is kind of the reason for if constexpr: comparing types or other things that aren't simply a set of values. So a switch constexpr does not make much sense to me. If anything, it would need another syntax (kind of like my example), and I'm not sure that it would be beneficial. It certainly isn't necessary.






          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%2f53379143%2fif-theres-an-if-constexpr-how-come-theres-no-switch-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









            13














            if constexpr was ultimately derived from a more sane form of the static if concept. Because of that derivation, applying the same idea to switch does not appear to have been considered by the standards committee. So this is likely the primary reason: nobody added it to the paper since it was a restricted form of a syntax where switch wouldn't have made sense.



            That being said, switch has a lot of baggage in it. The most notable bit being the automatic fallthrough behavior. That makes defining its behavior a bit problematic.



            See, one of the powers of if constexpr is to make the side not taken at compile time be discarded under certain conditions. This is an important part of the syntax. So a hypothetical switch constexpr would be expected to have similar powers.



            That's a lot harder to do with fallthrough, since the case blocks are not as fundamentally distinct as the two blocks of an if statement. Especially if you have conditional fallthrough. Now, you could make switch constexpr not have automatic fallthrough (or fallthrough at all), so that the different sections are distinct. But then you've subtly changed how the syntax works; a non-constexpr form of switch behaves differently from the constexpr form. That's not good.



            Yes, you could make it a compile error to not put break; statements between the labels.



            Note that the two main pattern-matching proposals, P1308 and P1260, specifically avoid using switch, instead inventing a new keyword. They both have constexpr aspects to them, but they make it abundantly clear that they are not switch/case.






            share|improve this answer



















            • 4





              While both proposals are interesting, P1308 introduces syntax that looks very little like current C++. P2160 is quite a fascinating paper.

              – Jon Harper
              Nov 19 '18 at 20:47






            • 3





              @JonHarper: "P1308 introduces syntax that looks very little like current C++." You know, normally I would have responded with "you say that as if it's a bad thing ;)". But after looking at that proposal, it's kinda scary in there.

              – Nicol Bolas
              Nov 19 '18 at 21:15






            • 1





              Excellent explanation and references. Haskell++, anyone?

              – cantordust
              Nov 20 '18 at 2:15






            • 1





              @NicolBolas [.scary true]

              – Yakk - Adam Nevraumont
              Nov 23 '18 at 19:08
















            13














            if constexpr was ultimately derived from a more sane form of the static if concept. Because of that derivation, applying the same idea to switch does not appear to have been considered by the standards committee. So this is likely the primary reason: nobody added it to the paper since it was a restricted form of a syntax where switch wouldn't have made sense.



            That being said, switch has a lot of baggage in it. The most notable bit being the automatic fallthrough behavior. That makes defining its behavior a bit problematic.



            See, one of the powers of if constexpr is to make the side not taken at compile time be discarded under certain conditions. This is an important part of the syntax. So a hypothetical switch constexpr would be expected to have similar powers.



            That's a lot harder to do with fallthrough, since the case blocks are not as fundamentally distinct as the two blocks of an if statement. Especially if you have conditional fallthrough. Now, you could make switch constexpr not have automatic fallthrough (or fallthrough at all), so that the different sections are distinct. But then you've subtly changed how the syntax works; a non-constexpr form of switch behaves differently from the constexpr form. That's not good.



            Yes, you could make it a compile error to not put break; statements between the labels.



            Note that the two main pattern-matching proposals, P1308 and P1260, specifically avoid using switch, instead inventing a new keyword. They both have constexpr aspects to them, but they make it abundantly clear that they are not switch/case.






            share|improve this answer



















            • 4





              While both proposals are interesting, P1308 introduces syntax that looks very little like current C++. P2160 is quite a fascinating paper.

              – Jon Harper
              Nov 19 '18 at 20:47






            • 3





              @JonHarper: "P1308 introduces syntax that looks very little like current C++." You know, normally I would have responded with "you say that as if it's a bad thing ;)". But after looking at that proposal, it's kinda scary in there.

              – Nicol Bolas
              Nov 19 '18 at 21:15






            • 1





              Excellent explanation and references. Haskell++, anyone?

              – cantordust
              Nov 20 '18 at 2:15






            • 1





              @NicolBolas [.scary true]

              – Yakk - Adam Nevraumont
              Nov 23 '18 at 19:08














            13












            13








            13







            if constexpr was ultimately derived from a more sane form of the static if concept. Because of that derivation, applying the same idea to switch does not appear to have been considered by the standards committee. So this is likely the primary reason: nobody added it to the paper since it was a restricted form of a syntax where switch wouldn't have made sense.



            That being said, switch has a lot of baggage in it. The most notable bit being the automatic fallthrough behavior. That makes defining its behavior a bit problematic.



            See, one of the powers of if constexpr is to make the side not taken at compile time be discarded under certain conditions. This is an important part of the syntax. So a hypothetical switch constexpr would be expected to have similar powers.



            That's a lot harder to do with fallthrough, since the case blocks are not as fundamentally distinct as the two blocks of an if statement. Especially if you have conditional fallthrough. Now, you could make switch constexpr not have automatic fallthrough (or fallthrough at all), so that the different sections are distinct. But then you've subtly changed how the syntax works; a non-constexpr form of switch behaves differently from the constexpr form. That's not good.



            Yes, you could make it a compile error to not put break; statements between the labels.



            Note that the two main pattern-matching proposals, P1308 and P1260, specifically avoid using switch, instead inventing a new keyword. They both have constexpr aspects to them, but they make it abundantly clear that they are not switch/case.






            share|improve this answer













            if constexpr was ultimately derived from a more sane form of the static if concept. Because of that derivation, applying the same idea to switch does not appear to have been considered by the standards committee. So this is likely the primary reason: nobody added it to the paper since it was a restricted form of a syntax where switch wouldn't have made sense.



            That being said, switch has a lot of baggage in it. The most notable bit being the automatic fallthrough behavior. That makes defining its behavior a bit problematic.



            See, one of the powers of if constexpr is to make the side not taken at compile time be discarded under certain conditions. This is an important part of the syntax. So a hypothetical switch constexpr would be expected to have similar powers.



            That's a lot harder to do with fallthrough, since the case blocks are not as fundamentally distinct as the two blocks of an if statement. Especially if you have conditional fallthrough. Now, you could make switch constexpr not have automatic fallthrough (or fallthrough at all), so that the different sections are distinct. But then you've subtly changed how the syntax works; a non-constexpr form of switch behaves differently from the constexpr form. That's not good.



            Yes, you could make it a compile error to not put break; statements between the labels.



            Note that the two main pattern-matching proposals, P1308 and P1260, specifically avoid using switch, instead inventing a new keyword. They both have constexpr aspects to them, but they make it abundantly clear that they are not switch/case.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 '18 at 17:29









            Nicol BolasNicol Bolas

            287k33478649




            287k33478649








            • 4





              While both proposals are interesting, P1308 introduces syntax that looks very little like current C++. P2160 is quite a fascinating paper.

              – Jon Harper
              Nov 19 '18 at 20:47






            • 3





              @JonHarper: "P1308 introduces syntax that looks very little like current C++." You know, normally I would have responded with "you say that as if it's a bad thing ;)". But after looking at that proposal, it's kinda scary in there.

              – Nicol Bolas
              Nov 19 '18 at 21:15






            • 1





              Excellent explanation and references. Haskell++, anyone?

              – cantordust
              Nov 20 '18 at 2:15






            • 1





              @NicolBolas [.scary true]

              – Yakk - Adam Nevraumont
              Nov 23 '18 at 19:08














            • 4





              While both proposals are interesting, P1308 introduces syntax that looks very little like current C++. P2160 is quite a fascinating paper.

              – Jon Harper
              Nov 19 '18 at 20:47






            • 3





              @JonHarper: "P1308 introduces syntax that looks very little like current C++." You know, normally I would have responded with "you say that as if it's a bad thing ;)". But after looking at that proposal, it's kinda scary in there.

              – Nicol Bolas
              Nov 19 '18 at 21:15






            • 1





              Excellent explanation and references. Haskell++, anyone?

              – cantordust
              Nov 20 '18 at 2:15






            • 1





              @NicolBolas [.scary true]

              – Yakk - Adam Nevraumont
              Nov 23 '18 at 19:08








            4




            4





            While both proposals are interesting, P1308 introduces syntax that looks very little like current C++. P2160 is quite a fascinating paper.

            – Jon Harper
            Nov 19 '18 at 20:47





            While both proposals are interesting, P1308 introduces syntax that looks very little like current C++. P2160 is quite a fascinating paper.

            – Jon Harper
            Nov 19 '18 at 20:47




            3




            3





            @JonHarper: "P1308 introduces syntax that looks very little like current C++." You know, normally I would have responded with "you say that as if it's a bad thing ;)". But after looking at that proposal, it's kinda scary in there.

            – Nicol Bolas
            Nov 19 '18 at 21:15





            @JonHarper: "P1308 introduces syntax that looks very little like current C++." You know, normally I would have responded with "you say that as if it's a bad thing ;)". But after looking at that proposal, it's kinda scary in there.

            – Nicol Bolas
            Nov 19 '18 at 21:15




            1




            1





            Excellent explanation and references. Haskell++, anyone?

            – cantordust
            Nov 20 '18 at 2:15





            Excellent explanation and references. Haskell++, anyone?

            – cantordust
            Nov 20 '18 at 2:15




            1




            1





            @NicolBolas [.scary true]

            – Yakk - Adam Nevraumont
            Nov 23 '18 at 19:08





            @NicolBolas [.scary true]

            – Yakk - Adam Nevraumont
            Nov 23 '18 at 19:08













            0














            I am not authoritative, but if you look at the selection statements if has a clear separation between true and false statements, switch doesn't.



            I presume it would be relatively harder to discard unused parts of switch from evaluation, especially since they can fall-through.



            Reimplementing switch as if-else-if is not so straight forward if you want to maintain all the (exotic) capabilities of it.






            share|improve this answer
























            • I'm sure there is a simple way to transform any switch into a tree of if branches, even considering fallthrough.

              – Nelfeal
              Nov 19 '18 at 17:15











            • See slight edit regarding the complexity of if

              – einpoklum
              Nov 19 '18 at 17:17






            • 1





              See Duff's device. switch is a lot less structured than most people think.

              – T.C.
              Nov 19 '18 at 19:41













            • @T.C. I am not sure who what were you commenting on, my answer or the 1st comment, but that is exactly my point. Because of such lack of the structure it would be pretty hard to reimplement it. It's way easier to base discarding rules on whole statement - using existing syntax, than coming up with custom rules for something amorphous.

              – luk32
              Nov 19 '18 at 22:04


















            0














            I am not authoritative, but if you look at the selection statements if has a clear separation between true and false statements, switch doesn't.



            I presume it would be relatively harder to discard unused parts of switch from evaluation, especially since they can fall-through.



            Reimplementing switch as if-else-if is not so straight forward if you want to maintain all the (exotic) capabilities of it.






            share|improve this answer
























            • I'm sure there is a simple way to transform any switch into a tree of if branches, even considering fallthrough.

              – Nelfeal
              Nov 19 '18 at 17:15











            • See slight edit regarding the complexity of if

              – einpoklum
              Nov 19 '18 at 17:17






            • 1





              See Duff's device. switch is a lot less structured than most people think.

              – T.C.
              Nov 19 '18 at 19:41













            • @T.C. I am not sure who what were you commenting on, my answer or the 1st comment, but that is exactly my point. Because of such lack of the structure it would be pretty hard to reimplement it. It's way easier to base discarding rules on whole statement - using existing syntax, than coming up with custom rules for something amorphous.

              – luk32
              Nov 19 '18 at 22:04
















            0












            0








            0







            I am not authoritative, but if you look at the selection statements if has a clear separation between true and false statements, switch doesn't.



            I presume it would be relatively harder to discard unused parts of switch from evaluation, especially since they can fall-through.



            Reimplementing switch as if-else-if is not so straight forward if you want to maintain all the (exotic) capabilities of it.






            share|improve this answer













            I am not authoritative, but if you look at the selection statements if has a clear separation between true and false statements, switch doesn't.



            I presume it would be relatively harder to discard unused parts of switch from evaluation, especially since they can fall-through.



            Reimplementing switch as if-else-if is not so straight forward if you want to maintain all the (exotic) capabilities of it.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 '18 at 17:11









            luk32luk32

            12.6k2547




            12.6k2547













            • I'm sure there is a simple way to transform any switch into a tree of if branches, even considering fallthrough.

              – Nelfeal
              Nov 19 '18 at 17:15











            • See slight edit regarding the complexity of if

              – einpoklum
              Nov 19 '18 at 17:17






            • 1





              See Duff's device. switch is a lot less structured than most people think.

              – T.C.
              Nov 19 '18 at 19:41













            • @T.C. I am not sure who what were you commenting on, my answer or the 1st comment, but that is exactly my point. Because of such lack of the structure it would be pretty hard to reimplement it. It's way easier to base discarding rules on whole statement - using existing syntax, than coming up with custom rules for something amorphous.

              – luk32
              Nov 19 '18 at 22:04





















            • I'm sure there is a simple way to transform any switch into a tree of if branches, even considering fallthrough.

              – Nelfeal
              Nov 19 '18 at 17:15











            • See slight edit regarding the complexity of if

              – einpoklum
              Nov 19 '18 at 17:17






            • 1





              See Duff's device. switch is a lot less structured than most people think.

              – T.C.
              Nov 19 '18 at 19:41













            • @T.C. I am not sure who what were you commenting on, my answer or the 1st comment, but that is exactly my point. Because of such lack of the structure it would be pretty hard to reimplement it. It's way easier to base discarding rules on whole statement - using existing syntax, than coming up with custom rules for something amorphous.

              – luk32
              Nov 19 '18 at 22:04



















            I'm sure there is a simple way to transform any switch into a tree of if branches, even considering fallthrough.

            – Nelfeal
            Nov 19 '18 at 17:15





            I'm sure there is a simple way to transform any switch into a tree of if branches, even considering fallthrough.

            – Nelfeal
            Nov 19 '18 at 17:15













            See slight edit regarding the complexity of if

            – einpoklum
            Nov 19 '18 at 17:17





            See slight edit regarding the complexity of if

            – einpoklum
            Nov 19 '18 at 17:17




            1




            1





            See Duff's device. switch is a lot less structured than most people think.

            – T.C.
            Nov 19 '18 at 19:41







            See Duff's device. switch is a lot less structured than most people think.

            – T.C.
            Nov 19 '18 at 19:41















            @T.C. I am not sure who what were you commenting on, my answer or the 1st comment, but that is exactly my point. Because of such lack of the structure it would be pretty hard to reimplement it. It's way easier to base discarding rules on whole statement - using existing syntax, than coming up with custom rules for something amorphous.

            – luk32
            Nov 19 '18 at 22:04







            @T.C. I am not sure who what were you commenting on, my answer or the 1st comment, but that is exactly my point. Because of such lack of the structure it would be pretty hard to reimplement it. It's way easier to base discarding rules on whole statement - using existing syntax, than coming up with custom rules for something amorphous.

            – luk32
            Nov 19 '18 at 22:04













            0














            Consider the following example from the answer to another question (about optimized if branches).



            struct Cat { void meow() { } };
            struct Dog { void bark() { } };

            template <typename T>
            void pet(T x)
            {
            if(std::is_same<T, Cat>{}){ x.meow(); }
            else if(std::is_same<T, Dog>{}){ x.bark(); }
            }

            pet(Cat{});
            pet(Dog{});


            You cannot replicate this with a switch because you would need the case values to actually be types; something like the following, which is impossible for obvious reasons.



            template <typename T>
            void pet(T x)
            {
            switch (T) {
            case Cat:
            x.meow();
            break;
            case Dog:
            x.meow();
            break;
            }
            }


            This example is kind of the reason for if constexpr: comparing types or other things that aren't simply a set of values. So a switch constexpr does not make much sense to me. If anything, it would need another syntax (kind of like my example), and I'm not sure that it would be beneficial. It certainly isn't necessary.






            share|improve this answer




























              0














              Consider the following example from the answer to another question (about optimized if branches).



              struct Cat { void meow() { } };
              struct Dog { void bark() { } };

              template <typename T>
              void pet(T x)
              {
              if(std::is_same<T, Cat>{}){ x.meow(); }
              else if(std::is_same<T, Dog>{}){ x.bark(); }
              }

              pet(Cat{});
              pet(Dog{});


              You cannot replicate this with a switch because you would need the case values to actually be types; something like the following, which is impossible for obvious reasons.



              template <typename T>
              void pet(T x)
              {
              switch (T) {
              case Cat:
              x.meow();
              break;
              case Dog:
              x.meow();
              break;
              }
              }


              This example is kind of the reason for if constexpr: comparing types or other things that aren't simply a set of values. So a switch constexpr does not make much sense to me. If anything, it would need another syntax (kind of like my example), and I'm not sure that it would be beneficial. It certainly isn't necessary.






              share|improve this answer


























                0












                0








                0







                Consider the following example from the answer to another question (about optimized if branches).



                struct Cat { void meow() { } };
                struct Dog { void bark() { } };

                template <typename T>
                void pet(T x)
                {
                if(std::is_same<T, Cat>{}){ x.meow(); }
                else if(std::is_same<T, Dog>{}){ x.bark(); }
                }

                pet(Cat{});
                pet(Dog{});


                You cannot replicate this with a switch because you would need the case values to actually be types; something like the following, which is impossible for obvious reasons.



                template <typename T>
                void pet(T x)
                {
                switch (T) {
                case Cat:
                x.meow();
                break;
                case Dog:
                x.meow();
                break;
                }
                }


                This example is kind of the reason for if constexpr: comparing types or other things that aren't simply a set of values. So a switch constexpr does not make much sense to me. If anything, it would need another syntax (kind of like my example), and I'm not sure that it would be beneficial. It certainly isn't necessary.






                share|improve this answer













                Consider the following example from the answer to another question (about optimized if branches).



                struct Cat { void meow() { } };
                struct Dog { void bark() { } };

                template <typename T>
                void pet(T x)
                {
                if(std::is_same<T, Cat>{}){ x.meow(); }
                else if(std::is_same<T, Dog>{}){ x.bark(); }
                }

                pet(Cat{});
                pet(Dog{});


                You cannot replicate this with a switch because you would need the case values to actually be types; something like the following, which is impossible for obvious reasons.



                template <typename T>
                void pet(T x)
                {
                switch (T) {
                case Cat:
                x.meow();
                break;
                case Dog:
                x.meow();
                break;
                }
                }


                This example is kind of the reason for if constexpr: comparing types or other things that aren't simply a set of values. So a switch constexpr does not make much sense to me. If anything, it would need another syntax (kind of like my example), and I'm not sure that it would be beneficial. It certainly isn't necessary.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 19 '18 at 17:14









                NelfealNelfeal

                5,1491825




                5,1491825






























                    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%2f53379143%2fif-theres-an-if-constexpr-how-come-theres-no-switch-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







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini