How to typedef template function pointer?









up vote
1
down vote

favorite












I want to typedef a function pointer that points to a template function.



class A

template <typename T>
typedef void (*FPTR)<T>();



I have tried in this way and didn't succeed. Any idea about this thing?










share|improve this question



















  • 1




    Can you make an example demonstrating how you want to use said typedef?
    – HolyBlackCat
    Oct 28 at 10:49











  • If neither parameters types nor return type of your template function depend on template parameters, then you can use a regular function pointer (typedef void (*FPTR)();, or even better: using FPTR = void (*)();). But that pointer can only point to a specific instantination of the function (e.g. FPTR ptr = foo<int>;). It's not possible to make a function pointer point to the function template itself, rather than one of if its instantinations.
    – HolyBlackCat
    Oct 28 at 10:52











  • @HolyBlackCat suppose I wrote down using FPTR = void (*)();. Can I refer my FPTR to any initialization of foo function? Let's say I have function that has different implementations for each kind of int types like foo<int8>() ... foo<int16>() ... foo<int32>() .... Can I refer my pointer to these function implementations?
    – Arsen Gharagyozyan
    Oct 28 at 10:57











  • As I said, if neither parameters types nor return type of your template function depend on the template parameter, then you can use a function pointer. It's not clear from your comment if that's the case or not.
    – HolyBlackCat
    Oct 28 at 11:02











  • Ok thank you. Yea it is. There's no return type or arguments depending on template parameters.
    – Arsen Gharagyozyan
    Oct 28 at 11:06














up vote
1
down vote

favorite












I want to typedef a function pointer that points to a template function.



class A

template <typename T>
typedef void (*FPTR)<T>();



I have tried in this way and didn't succeed. Any idea about this thing?










share|improve this question



















  • 1




    Can you make an example demonstrating how you want to use said typedef?
    – HolyBlackCat
    Oct 28 at 10:49











  • If neither parameters types nor return type of your template function depend on template parameters, then you can use a regular function pointer (typedef void (*FPTR)();, or even better: using FPTR = void (*)();). But that pointer can only point to a specific instantination of the function (e.g. FPTR ptr = foo<int>;). It's not possible to make a function pointer point to the function template itself, rather than one of if its instantinations.
    – HolyBlackCat
    Oct 28 at 10:52











  • @HolyBlackCat suppose I wrote down using FPTR = void (*)();. Can I refer my FPTR to any initialization of foo function? Let's say I have function that has different implementations for each kind of int types like foo<int8>() ... foo<int16>() ... foo<int32>() .... Can I refer my pointer to these function implementations?
    – Arsen Gharagyozyan
    Oct 28 at 10:57











  • As I said, if neither parameters types nor return type of your template function depend on the template parameter, then you can use a function pointer. It's not clear from your comment if that's the case or not.
    – HolyBlackCat
    Oct 28 at 11:02











  • Ok thank you. Yea it is. There's no return type or arguments depending on template parameters.
    – Arsen Gharagyozyan
    Oct 28 at 11:06












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I want to typedef a function pointer that points to a template function.



class A

template <typename T>
typedef void (*FPTR)<T>();



I have tried in this way and didn't succeed. Any idea about this thing?










share|improve this question















I want to typedef a function pointer that points to a template function.



class A

template <typename T>
typedef void (*FPTR)<T>();



I have tried in this way and didn't succeed. Any idea about this thing?







c++ c++11 templates function-pointers typedef






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 8:20









JeJo

3,7763624




3,7763624










asked Oct 28 at 10:46









Arsen Gharagyozyan

334




334







  • 1




    Can you make an example demonstrating how you want to use said typedef?
    – HolyBlackCat
    Oct 28 at 10:49











  • If neither parameters types nor return type of your template function depend on template parameters, then you can use a regular function pointer (typedef void (*FPTR)();, or even better: using FPTR = void (*)();). But that pointer can only point to a specific instantination of the function (e.g. FPTR ptr = foo<int>;). It's not possible to make a function pointer point to the function template itself, rather than one of if its instantinations.
    – HolyBlackCat
    Oct 28 at 10:52











  • @HolyBlackCat suppose I wrote down using FPTR = void (*)();. Can I refer my FPTR to any initialization of foo function? Let's say I have function that has different implementations for each kind of int types like foo<int8>() ... foo<int16>() ... foo<int32>() .... Can I refer my pointer to these function implementations?
    – Arsen Gharagyozyan
    Oct 28 at 10:57











  • As I said, if neither parameters types nor return type of your template function depend on the template parameter, then you can use a function pointer. It's not clear from your comment if that's the case or not.
    – HolyBlackCat
    Oct 28 at 11:02











  • Ok thank you. Yea it is. There's no return type or arguments depending on template parameters.
    – Arsen Gharagyozyan
    Oct 28 at 11:06












  • 1




    Can you make an example demonstrating how you want to use said typedef?
    – HolyBlackCat
    Oct 28 at 10:49











  • If neither parameters types nor return type of your template function depend on template parameters, then you can use a regular function pointer (typedef void (*FPTR)();, or even better: using FPTR = void (*)();). But that pointer can only point to a specific instantination of the function (e.g. FPTR ptr = foo<int>;). It's not possible to make a function pointer point to the function template itself, rather than one of if its instantinations.
    – HolyBlackCat
    Oct 28 at 10:52











  • @HolyBlackCat suppose I wrote down using FPTR = void (*)();. Can I refer my FPTR to any initialization of foo function? Let's say I have function that has different implementations for each kind of int types like foo<int8>() ... foo<int16>() ... foo<int32>() .... Can I refer my pointer to these function implementations?
    – Arsen Gharagyozyan
    Oct 28 at 10:57











  • As I said, if neither parameters types nor return type of your template function depend on the template parameter, then you can use a function pointer. It's not clear from your comment if that's the case or not.
    – HolyBlackCat
    Oct 28 at 11:02











  • Ok thank you. Yea it is. There's no return type or arguments depending on template parameters.
    – Arsen Gharagyozyan
    Oct 28 at 11:06







1




1




Can you make an example demonstrating how you want to use said typedef?
– HolyBlackCat
Oct 28 at 10:49





Can you make an example demonstrating how you want to use said typedef?
– HolyBlackCat
Oct 28 at 10:49













If neither parameters types nor return type of your template function depend on template parameters, then you can use a regular function pointer (typedef void (*FPTR)();, or even better: using FPTR = void (*)();). But that pointer can only point to a specific instantination of the function (e.g. FPTR ptr = foo<int>;). It's not possible to make a function pointer point to the function template itself, rather than one of if its instantinations.
– HolyBlackCat
Oct 28 at 10:52





If neither parameters types nor return type of your template function depend on template parameters, then you can use a regular function pointer (typedef void (*FPTR)();, or even better: using FPTR = void (*)();). But that pointer can only point to a specific instantination of the function (e.g. FPTR ptr = foo<int>;). It's not possible to make a function pointer point to the function template itself, rather than one of if its instantinations.
– HolyBlackCat
Oct 28 at 10:52













@HolyBlackCat suppose I wrote down using FPTR = void (*)();. Can I refer my FPTR to any initialization of foo function? Let's say I have function that has different implementations for each kind of int types like foo<int8>() ... foo<int16>() ... foo<int32>() .... Can I refer my pointer to these function implementations?
– Arsen Gharagyozyan
Oct 28 at 10:57





@HolyBlackCat suppose I wrote down using FPTR = void (*)();. Can I refer my FPTR to any initialization of foo function? Let's say I have function that has different implementations for each kind of int types like foo<int8>() ... foo<int16>() ... foo<int32>() .... Can I refer my pointer to these function implementations?
– Arsen Gharagyozyan
Oct 28 at 10:57













As I said, if neither parameters types nor return type of your template function depend on the template parameter, then you can use a function pointer. It's not clear from your comment if that's the case or not.
– HolyBlackCat
Oct 28 at 11:02





As I said, if neither parameters types nor return type of your template function depend on the template parameter, then you can use a function pointer. It's not clear from your comment if that's the case or not.
– HolyBlackCat
Oct 28 at 11:02













Ok thank you. Yea it is. There's no return type or arguments depending on template parameters.
– Arsen Gharagyozyan
Oct 28 at 11:06




Ok thank you. Yea it is. There's no return type or arguments depending on template parameters.
– Arsen Gharagyozyan
Oct 28 at 11:06












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










As @HolyBlackCat pointed out, the normal function pointer should work as you have a simple templated void function, whose template parameter does not act on both return and argument types.



template <typename T>
void someVoidFunction()

using fPtrType = void(*)();

int main()

fPtrType funPtr1 = &someVoidFunction<int>;
fPtrType funPtr2 = &someVoidFunction<float>;
fPtrType funPtr3 = &someVoidFunction<std::string>;
return 0;



If it was the case, that template parameters depends on the function arg and return types you should have instantiated the function pointer as well for each kind.



template <typename T, typename U>
T someFunction(U u)

template <typename T, typename U>
using fPtrType = T(*)(U);

int main()

fPtrType<int, float> funPtr1 = &someFunction<int, float>; // instance 1
fPtrType<float, float> funPtr2 = &someFunction<float, float>; // instance 2
return 0;






share|improve this answer



























    up vote
    2
    down vote













    Template functions produce functions. Template classes produce classes. Template variables produce variables.



    Pointers can point at functions or variables. They cannot point at templates; templates have no address.



    Typedef defines the type of a variable.



    A template variable pointer could collectively point at various instances of a template function, but the initial binding would be done at compile time, and could only be aimed somewhere else one variable at a time.






    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%2f53030633%2fhow-to-typedef-template-function-pointer%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
      2
      down vote



      accepted










      As @HolyBlackCat pointed out, the normal function pointer should work as you have a simple templated void function, whose template parameter does not act on both return and argument types.



      template <typename T>
      void someVoidFunction()

      using fPtrType = void(*)();

      int main()

      fPtrType funPtr1 = &someVoidFunction<int>;
      fPtrType funPtr2 = &someVoidFunction<float>;
      fPtrType funPtr3 = &someVoidFunction<std::string>;
      return 0;



      If it was the case, that template parameters depends on the function arg and return types you should have instantiated the function pointer as well for each kind.



      template <typename T, typename U>
      T someFunction(U u)

      template <typename T, typename U>
      using fPtrType = T(*)(U);

      int main()

      fPtrType<int, float> funPtr1 = &someFunction<int, float>; // instance 1
      fPtrType<float, float> funPtr2 = &someFunction<float, float>; // instance 2
      return 0;






      share|improve this answer
























        up vote
        2
        down vote



        accepted










        As @HolyBlackCat pointed out, the normal function pointer should work as you have a simple templated void function, whose template parameter does not act on both return and argument types.



        template <typename T>
        void someVoidFunction()

        using fPtrType = void(*)();

        int main()

        fPtrType funPtr1 = &someVoidFunction<int>;
        fPtrType funPtr2 = &someVoidFunction<float>;
        fPtrType funPtr3 = &someVoidFunction<std::string>;
        return 0;



        If it was the case, that template parameters depends on the function arg and return types you should have instantiated the function pointer as well for each kind.



        template <typename T, typename U>
        T someFunction(U u)

        template <typename T, typename U>
        using fPtrType = T(*)(U);

        int main()

        fPtrType<int, float> funPtr1 = &someFunction<int, float>; // instance 1
        fPtrType<float, float> funPtr2 = &someFunction<float, float>; // instance 2
        return 0;






        share|improve this answer






















          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          As @HolyBlackCat pointed out, the normal function pointer should work as you have a simple templated void function, whose template parameter does not act on both return and argument types.



          template <typename T>
          void someVoidFunction()

          using fPtrType = void(*)();

          int main()

          fPtrType funPtr1 = &someVoidFunction<int>;
          fPtrType funPtr2 = &someVoidFunction<float>;
          fPtrType funPtr3 = &someVoidFunction<std::string>;
          return 0;



          If it was the case, that template parameters depends on the function arg and return types you should have instantiated the function pointer as well for each kind.



          template <typename T, typename U>
          T someFunction(U u)

          template <typename T, typename U>
          using fPtrType = T(*)(U);

          int main()

          fPtrType<int, float> funPtr1 = &someFunction<int, float>; // instance 1
          fPtrType<float, float> funPtr2 = &someFunction<float, float>; // instance 2
          return 0;






          share|improve this answer












          As @HolyBlackCat pointed out, the normal function pointer should work as you have a simple templated void function, whose template parameter does not act on both return and argument types.



          template <typename T>
          void someVoidFunction()

          using fPtrType = void(*)();

          int main()

          fPtrType funPtr1 = &someVoidFunction<int>;
          fPtrType funPtr2 = &someVoidFunction<float>;
          fPtrType funPtr3 = &someVoidFunction<std::string>;
          return 0;



          If it was the case, that template parameters depends on the function arg and return types you should have instantiated the function pointer as well for each kind.



          template <typename T, typename U>
          T someFunction(U u)

          template <typename T, typename U>
          using fPtrType = T(*)(U);

          int main()

          fPtrType<int, float> funPtr1 = &someFunction<int, float>; // instance 1
          fPtrType<float, float> funPtr2 = &someFunction<float, float>; // instance 2
          return 0;







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 28 at 11:09









          JeJo

          3,7763624




          3,7763624






















              up vote
              2
              down vote













              Template functions produce functions. Template classes produce classes. Template variables produce variables.



              Pointers can point at functions or variables. They cannot point at templates; templates have no address.



              Typedef defines the type of a variable.



              A template variable pointer could collectively point at various instances of a template function, but the initial binding would be done at compile time, and could only be aimed somewhere else one variable at a time.






              share|improve this answer
























                up vote
                2
                down vote













                Template functions produce functions. Template classes produce classes. Template variables produce variables.



                Pointers can point at functions or variables. They cannot point at templates; templates have no address.



                Typedef defines the type of a variable.



                A template variable pointer could collectively point at various instances of a template function, but the initial binding would be done at compile time, and could only be aimed somewhere else one variable at a time.






                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Template functions produce functions. Template classes produce classes. Template variables produce variables.



                  Pointers can point at functions or variables. They cannot point at templates; templates have no address.



                  Typedef defines the type of a variable.



                  A template variable pointer could collectively point at various instances of a template function, but the initial binding would be done at compile time, and could only be aimed somewhere else one variable at a time.






                  share|improve this answer












                  Template functions produce functions. Template classes produce classes. Template variables produce variables.



                  Pointers can point at functions or variables. They cannot point at templates; templates have no address.



                  Typedef defines the type of a variable.



                  A template variable pointer could collectively point at various instances of a template function, but the initial binding would be done at compile time, and could only be aimed somewhere else one variable at a time.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 28 at 11:08









                  Yakk - Adam Nevraumont

                  179k19187367




                  179k19187367



























                      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%2f53030633%2fhow-to-typedef-template-function-pointer%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

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線