What's the syntax to partially specialize a parmeter pack argument for void type?












6















I can't find a way to get this to work. Is it even possible? I don't see why it wouldn't be.



template <auto id, typename FirstField, typename... OtherFields>
struct FieldTypeById {
using Type = int;
};

template <auto id>
struct FieldTypeById<id, void> {
using Type = void;
};


int main()
{
using t1 = FieldTypeById<0, int>::Type;
using t2 = FieldTypeById<1>::Type;

return 0;
}


https://godbolt.org/z/AggnDq










share|improve this question



























    6















    I can't find a way to get this to work. Is it even possible? I don't see why it wouldn't be.



    template <auto id, typename FirstField, typename... OtherFields>
    struct FieldTypeById {
    using Type = int;
    };

    template <auto id>
    struct FieldTypeById<id, void> {
    using Type = void;
    };


    int main()
    {
    using t1 = FieldTypeById<0, int>::Type;
    using t2 = FieldTypeById<1>::Type;

    return 0;
    }


    https://godbolt.org/z/AggnDq










    share|improve this question

























      6












      6








      6


      1






      I can't find a way to get this to work. Is it even possible? I don't see why it wouldn't be.



      template <auto id, typename FirstField, typename... OtherFields>
      struct FieldTypeById {
      using Type = int;
      };

      template <auto id>
      struct FieldTypeById<id, void> {
      using Type = void;
      };


      int main()
      {
      using t1 = FieldTypeById<0, int>::Type;
      using t2 = FieldTypeById<1>::Type;

      return 0;
      }


      https://godbolt.org/z/AggnDq










      share|improve this question














      I can't find a way to get this to work. Is it even possible? I don't see why it wouldn't be.



      template <auto id, typename FirstField, typename... OtherFields>
      struct FieldTypeById {
      using Type = int;
      };

      template <auto id>
      struct FieldTypeById<id, void> {
      using Type = void;
      };


      int main()
      {
      using t1 = FieldTypeById<0, int>::Type;
      using t2 = FieldTypeById<1>::Type;

      return 0;
      }


      https://godbolt.org/z/AggnDq







      c++ templates c++17 template-specialization






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 8:02









      Violet GiraffeViolet Giraffe

      14.6k28135246




      14.6k28135246
























          1 Answer
          1






          active

          oldest

          votes


















          8














          The problem in your example isn't the specialization, it's fine. The problem is that FieldTypeById<1> cannot deduce the type FirstField is. You can amend that by simply adding a default to the primary template:



          template <auto id, typename FirstField = void, typename... OtherFields>
          struct FieldTypeById {
          using Type = int;
          };


          Now all arguments are given explicitly, taken from defaults, or deduced (as an empty pack). After all the arguments are known, the specialization for those arguments can be used.



          See it live






          share|improve this answer


























          • Hmmm. If the "main" template has FirstField = void, meaning that it can be directly matched with FieldTypeById<1>, is the specialization ever going to be instantiated? I can now see that I can achieve the same goal using just one template with = void and std::conditional inside it, but still, I wonder if it can be done via a specialization.

            – Violet Giraffe
            Nov 22 '18 at 8:08








          • 2





            @VioletGiraffe - The specialization is instantiated when the arguments to the template match it. But default arguments can only appear on the primary template, because that's where the pattern matching starts (to explain it colloquially).

            – StoryTeller
            Nov 22 '18 at 8:10













          • Do I understand correctly that in order to specialize a template, one must first make sure that the desired specialized instantiation can be matched to the primary template?

            – Violet Giraffe
            Nov 22 '18 at 8:19











          • @StoryTeller : is that due to fact that when we pass argument list in the template there is no type deduction (unlike function template) ? I thought about that !!

            – Blood-HaZaRd
            Nov 22 '18 at 8:19








          • 1





            Thank you, that's an important thing to learn that I somehow missed during my many years of using C++!

            – Violet Giraffe
            Nov 22 '18 at 8:22











          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%2f53426296%2fwhats-the-syntax-to-partially-specialize-a-parmeter-pack-argument-for-void-type%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









          8














          The problem in your example isn't the specialization, it's fine. The problem is that FieldTypeById<1> cannot deduce the type FirstField is. You can amend that by simply adding a default to the primary template:



          template <auto id, typename FirstField = void, typename... OtherFields>
          struct FieldTypeById {
          using Type = int;
          };


          Now all arguments are given explicitly, taken from defaults, or deduced (as an empty pack). After all the arguments are known, the specialization for those arguments can be used.



          See it live






          share|improve this answer


























          • Hmmm. If the "main" template has FirstField = void, meaning that it can be directly matched with FieldTypeById<1>, is the specialization ever going to be instantiated? I can now see that I can achieve the same goal using just one template with = void and std::conditional inside it, but still, I wonder if it can be done via a specialization.

            – Violet Giraffe
            Nov 22 '18 at 8:08








          • 2





            @VioletGiraffe - The specialization is instantiated when the arguments to the template match it. But default arguments can only appear on the primary template, because that's where the pattern matching starts (to explain it colloquially).

            – StoryTeller
            Nov 22 '18 at 8:10













          • Do I understand correctly that in order to specialize a template, one must first make sure that the desired specialized instantiation can be matched to the primary template?

            – Violet Giraffe
            Nov 22 '18 at 8:19











          • @StoryTeller : is that due to fact that when we pass argument list in the template there is no type deduction (unlike function template) ? I thought about that !!

            – Blood-HaZaRd
            Nov 22 '18 at 8:19








          • 1





            Thank you, that's an important thing to learn that I somehow missed during my many years of using C++!

            – Violet Giraffe
            Nov 22 '18 at 8:22
















          8














          The problem in your example isn't the specialization, it's fine. The problem is that FieldTypeById<1> cannot deduce the type FirstField is. You can amend that by simply adding a default to the primary template:



          template <auto id, typename FirstField = void, typename... OtherFields>
          struct FieldTypeById {
          using Type = int;
          };


          Now all arguments are given explicitly, taken from defaults, or deduced (as an empty pack). After all the arguments are known, the specialization for those arguments can be used.



          See it live






          share|improve this answer


























          • Hmmm. If the "main" template has FirstField = void, meaning that it can be directly matched with FieldTypeById<1>, is the specialization ever going to be instantiated? I can now see that I can achieve the same goal using just one template with = void and std::conditional inside it, but still, I wonder if it can be done via a specialization.

            – Violet Giraffe
            Nov 22 '18 at 8:08








          • 2





            @VioletGiraffe - The specialization is instantiated when the arguments to the template match it. But default arguments can only appear on the primary template, because that's where the pattern matching starts (to explain it colloquially).

            – StoryTeller
            Nov 22 '18 at 8:10













          • Do I understand correctly that in order to specialize a template, one must first make sure that the desired specialized instantiation can be matched to the primary template?

            – Violet Giraffe
            Nov 22 '18 at 8:19











          • @StoryTeller : is that due to fact that when we pass argument list in the template there is no type deduction (unlike function template) ? I thought about that !!

            – Blood-HaZaRd
            Nov 22 '18 at 8:19








          • 1





            Thank you, that's an important thing to learn that I somehow missed during my many years of using C++!

            – Violet Giraffe
            Nov 22 '18 at 8:22














          8












          8








          8







          The problem in your example isn't the specialization, it's fine. The problem is that FieldTypeById<1> cannot deduce the type FirstField is. You can amend that by simply adding a default to the primary template:



          template <auto id, typename FirstField = void, typename... OtherFields>
          struct FieldTypeById {
          using Type = int;
          };


          Now all arguments are given explicitly, taken from defaults, or deduced (as an empty pack). After all the arguments are known, the specialization for those arguments can be used.



          See it live






          share|improve this answer















          The problem in your example isn't the specialization, it's fine. The problem is that FieldTypeById<1> cannot deduce the type FirstField is. You can amend that by simply adding a default to the primary template:



          template <auto id, typename FirstField = void, typename... OtherFields>
          struct FieldTypeById {
          using Type = int;
          };


          Now all arguments are given explicitly, taken from defaults, or deduced (as an empty pack). After all the arguments are known, the specialization for those arguments can be used.



          See it live







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 8:13

























          answered Nov 22 '18 at 8:04









          StoryTellerStoryTeller

          99.8k12201271




          99.8k12201271













          • Hmmm. If the "main" template has FirstField = void, meaning that it can be directly matched with FieldTypeById<1>, is the specialization ever going to be instantiated? I can now see that I can achieve the same goal using just one template with = void and std::conditional inside it, but still, I wonder if it can be done via a specialization.

            – Violet Giraffe
            Nov 22 '18 at 8:08








          • 2





            @VioletGiraffe - The specialization is instantiated when the arguments to the template match it. But default arguments can only appear on the primary template, because that's where the pattern matching starts (to explain it colloquially).

            – StoryTeller
            Nov 22 '18 at 8:10













          • Do I understand correctly that in order to specialize a template, one must first make sure that the desired specialized instantiation can be matched to the primary template?

            – Violet Giraffe
            Nov 22 '18 at 8:19











          • @StoryTeller : is that due to fact that when we pass argument list in the template there is no type deduction (unlike function template) ? I thought about that !!

            – Blood-HaZaRd
            Nov 22 '18 at 8:19








          • 1





            Thank you, that's an important thing to learn that I somehow missed during my many years of using C++!

            – Violet Giraffe
            Nov 22 '18 at 8:22



















          • Hmmm. If the "main" template has FirstField = void, meaning that it can be directly matched with FieldTypeById<1>, is the specialization ever going to be instantiated? I can now see that I can achieve the same goal using just one template with = void and std::conditional inside it, but still, I wonder if it can be done via a specialization.

            – Violet Giraffe
            Nov 22 '18 at 8:08








          • 2





            @VioletGiraffe - The specialization is instantiated when the arguments to the template match it. But default arguments can only appear on the primary template, because that's where the pattern matching starts (to explain it colloquially).

            – StoryTeller
            Nov 22 '18 at 8:10













          • Do I understand correctly that in order to specialize a template, one must first make sure that the desired specialized instantiation can be matched to the primary template?

            – Violet Giraffe
            Nov 22 '18 at 8:19











          • @StoryTeller : is that due to fact that when we pass argument list in the template there is no type deduction (unlike function template) ? I thought about that !!

            – Blood-HaZaRd
            Nov 22 '18 at 8:19








          • 1





            Thank you, that's an important thing to learn that I somehow missed during my many years of using C++!

            – Violet Giraffe
            Nov 22 '18 at 8:22

















          Hmmm. If the "main" template has FirstField = void, meaning that it can be directly matched with FieldTypeById<1>, is the specialization ever going to be instantiated? I can now see that I can achieve the same goal using just one template with = void and std::conditional inside it, but still, I wonder if it can be done via a specialization.

          – Violet Giraffe
          Nov 22 '18 at 8:08







          Hmmm. If the "main" template has FirstField = void, meaning that it can be directly matched with FieldTypeById<1>, is the specialization ever going to be instantiated? I can now see that I can achieve the same goal using just one template with = void and std::conditional inside it, but still, I wonder if it can be done via a specialization.

          – Violet Giraffe
          Nov 22 '18 at 8:08






          2




          2





          @VioletGiraffe - The specialization is instantiated when the arguments to the template match it. But default arguments can only appear on the primary template, because that's where the pattern matching starts (to explain it colloquially).

          – StoryTeller
          Nov 22 '18 at 8:10







          @VioletGiraffe - The specialization is instantiated when the arguments to the template match it. But default arguments can only appear on the primary template, because that's where the pattern matching starts (to explain it colloquially).

          – StoryTeller
          Nov 22 '18 at 8:10















          Do I understand correctly that in order to specialize a template, one must first make sure that the desired specialized instantiation can be matched to the primary template?

          – Violet Giraffe
          Nov 22 '18 at 8:19





          Do I understand correctly that in order to specialize a template, one must first make sure that the desired specialized instantiation can be matched to the primary template?

          – Violet Giraffe
          Nov 22 '18 at 8:19













          @StoryTeller : is that due to fact that when we pass argument list in the template there is no type deduction (unlike function template) ? I thought about that !!

          – Blood-HaZaRd
          Nov 22 '18 at 8:19







          @StoryTeller : is that due to fact that when we pass argument list in the template there is no type deduction (unlike function template) ? I thought about that !!

          – Blood-HaZaRd
          Nov 22 '18 at 8:19






          1




          1





          Thank you, that's an important thing to learn that I somehow missed during my many years of using C++!

          – Violet Giraffe
          Nov 22 '18 at 8:22





          Thank you, that's an important thing to learn that I somehow missed during my many years of using C++!

          – Violet Giraffe
          Nov 22 '18 at 8:22




















          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%2f53426296%2fwhats-the-syntax-to-partially-specialize-a-parmeter-pack-argument-for-void-type%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

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          'app-layout' is not a known element: how to share Component with different Modules