Generate function with signature of pointer's type passed to template2019 Community Moderator ElectionC++: No matching function call when calling tuple_transpose functionSpecializing class for generic function returning pointer to functionSpecialize function template with decltype trailing return typeC++14 auto lambda can accept Obj<std::tuple<void> > — but template functions cannot?How to extract lambda's Return Type and Variadic Parameters Pack back from general template<typename T>Generic object with common baseclass to std::functionClang can't find template binary operator in fold expressionAutomatic return types for function pointer arguments?Call lambda with other lambda result when its return type is voidPassing variadic template to pthread_create

Writing in a Christian voice

Geography in 3D perspective

How could an airship be repaired midflight?

Should I use acronyms in dialogues before telling the readers what it stands for in fiction?

Synchronized implementation of a bank account in Java

How does 取材で訪れた integrate into this sentence?

HP P840 HDD RAID 5 many strange drive failures

What is the relationship between relativity and the Doppler effect?

Deletion of copy-ctor & copy-assignment - public, private or protected?

What does Jesus mean regarding "Raca," and "you fool?" - is he contrasting them?

Is there a term for accumulated dirt on the outside of your hands and feet?

Loading the leaflet Map in Lightning Web Component

Can a medieval gyroplane be built?

Should I be concerned about student access to a test bank?

두음법칙 - When did North and South diverge in pronunciation of initial ㄹ?

Help prove this basic trig identity please!

Pronounciation of the combination "st" in spanish accents

If "dar" means "to give", what does "daros" mean?

Light propagating through a sound wave

Relation between independence and correlation of uniform random variables

I got the following comment from a reputed math journal. What does it mean?

Recruiter wants very extensive technical details about all of my previous work

Is it true that good novels will automatically sell themselves on Amazon (and so on) and there is no need for one to waste time promoting?

Comment Box for Substitution Method of Integrals



Generate function with signature of pointer's type passed to template



2019 Community Moderator ElectionC++: No matching function call when calling tuple_transpose functionSpecializing class for generic function returning pointer to functionSpecialize function template with decltype trailing return typeC++14 auto lambda can accept Obj<std::tuple<void> > — but template functions cannot?How to extract lambda's Return Type and Variadic Parameters Pack back from general template<typename T>Generic object with common baseclass to std::functionClang can't find template binary operator in fold expressionAutomatic return types for function pointer arguments?Call lambda with other lambda result when its return type is voidPassing variadic template to pthread_create










0















I want to create a generator which for each pointer to function will create a static function to which the pointer can point to:



template <auto &PtrToIsrHandler, auto MemberFunction> struct enable_member_isr_handler

using TypeOfCFunPtr = std::remove_reference_t<decltype(PtrToIsrHandler)>;
using TypeOfCFunPointed = std::remove_pointer_t<TypeOfCFunPtr>;
using RetType = std::invoke_result_t<TypeOfCFunPointed>;
static RetType isr_handler(/* dunno what to put here */)

return /* call MemberFunction here somehow */;


enable_member_isr_handler()

PtrToIsrHandler = isr_handler;


;


In the parameter list of the isr_handler method I tried to put a tuple by using:



template <typename T> struct get_parameters;
template <typename Ret, typename... Args> struct get_parameters<Ret(Args...)>

using args_t = std::tuple<Args...>;
;


But I get then:



error: invalid conversion from void (*)(get_parameters<void()>)}’ to ‘void (*)()’


How can I make it in such a way that PtrToIsrHandler could be a valid pointer to isr_handler?










share|improve this question






















  • This seems like an XY problem. What's the actual problem you're trying to solve?

    – Barry
    Mar 6 at 22:14











  • If you want to call a member function you need an instance to call it on. I don't see one anywhere in your example. Other then that I think you are looking for partial template specialization. You can pass on MemberFunction and decltype(MemberFunction) and use partial specialization to pick up return value and the argument list.

    – super
    Mar 6 at 22:51











  • Might be able to help if you can post the rough syntax for how you would like to use your class. Mainly show how you instantiate enable_member_isr_handler and how you intend to call isr_handler.

    – Always Confused
    Mar 6 at 22:53












  • @Barry of course not, this is a bigger problem, but I always split it into smaller problems and almost every problem I try to solve is an XY problem. I'm just experimenting and I try to see whether it can work or not.

    – K. Koovalsky
    Mar 7 at 6:05











  • @super the question is really about, how to define a function with the same signature as other function. The main problem I have is how to pick the argument list.

    – K. Koovalsky
    Mar 7 at 6:05















0















I want to create a generator which for each pointer to function will create a static function to which the pointer can point to:



template <auto &PtrToIsrHandler, auto MemberFunction> struct enable_member_isr_handler

using TypeOfCFunPtr = std::remove_reference_t<decltype(PtrToIsrHandler)>;
using TypeOfCFunPointed = std::remove_pointer_t<TypeOfCFunPtr>;
using RetType = std::invoke_result_t<TypeOfCFunPointed>;
static RetType isr_handler(/* dunno what to put here */)

return /* call MemberFunction here somehow */;


enable_member_isr_handler()

PtrToIsrHandler = isr_handler;


;


In the parameter list of the isr_handler method I tried to put a tuple by using:



template <typename T> struct get_parameters;
template <typename Ret, typename... Args> struct get_parameters<Ret(Args...)>

using args_t = std::tuple<Args...>;
;


But I get then:



error: invalid conversion from void (*)(get_parameters<void()>)}’ to ‘void (*)()’


How can I make it in such a way that PtrToIsrHandler could be a valid pointer to isr_handler?










share|improve this question






















  • This seems like an XY problem. What's the actual problem you're trying to solve?

    – Barry
    Mar 6 at 22:14











  • If you want to call a member function you need an instance to call it on. I don't see one anywhere in your example. Other then that I think you are looking for partial template specialization. You can pass on MemberFunction and decltype(MemberFunction) and use partial specialization to pick up return value and the argument list.

    – super
    Mar 6 at 22:51











  • Might be able to help if you can post the rough syntax for how you would like to use your class. Mainly show how you instantiate enable_member_isr_handler and how you intend to call isr_handler.

    – Always Confused
    Mar 6 at 22:53












  • @Barry of course not, this is a bigger problem, but I always split it into smaller problems and almost every problem I try to solve is an XY problem. I'm just experimenting and I try to see whether it can work or not.

    – K. Koovalsky
    Mar 7 at 6:05











  • @super the question is really about, how to define a function with the same signature as other function. The main problem I have is how to pick the argument list.

    – K. Koovalsky
    Mar 7 at 6:05













0












0








0








I want to create a generator which for each pointer to function will create a static function to which the pointer can point to:



template <auto &PtrToIsrHandler, auto MemberFunction> struct enable_member_isr_handler

using TypeOfCFunPtr = std::remove_reference_t<decltype(PtrToIsrHandler)>;
using TypeOfCFunPointed = std::remove_pointer_t<TypeOfCFunPtr>;
using RetType = std::invoke_result_t<TypeOfCFunPointed>;
static RetType isr_handler(/* dunno what to put here */)

return /* call MemberFunction here somehow */;


enable_member_isr_handler()

PtrToIsrHandler = isr_handler;


;


In the parameter list of the isr_handler method I tried to put a tuple by using:



template <typename T> struct get_parameters;
template <typename Ret, typename... Args> struct get_parameters<Ret(Args...)>

using args_t = std::tuple<Args...>;
;


But I get then:



error: invalid conversion from void (*)(get_parameters<void()>)}’ to ‘void (*)()’


How can I make it in such a way that PtrToIsrHandler could be a valid pointer to isr_handler?










share|improve this question














I want to create a generator which for each pointer to function will create a static function to which the pointer can point to:



template <auto &PtrToIsrHandler, auto MemberFunction> struct enable_member_isr_handler

using TypeOfCFunPtr = std::remove_reference_t<decltype(PtrToIsrHandler)>;
using TypeOfCFunPointed = std::remove_pointer_t<TypeOfCFunPtr>;
using RetType = std::invoke_result_t<TypeOfCFunPointed>;
static RetType isr_handler(/* dunno what to put here */)

return /* call MemberFunction here somehow */;


enable_member_isr_handler()

PtrToIsrHandler = isr_handler;


;


In the parameter list of the isr_handler method I tried to put a tuple by using:



template <typename T> struct get_parameters;
template <typename Ret, typename... Args> struct get_parameters<Ret(Args...)>

using args_t = std::tuple<Args...>;
;


But I get then:



error: invalid conversion from void (*)(get_parameters<void()>)}’ to ‘void (*)()’


How can I make it in such a way that PtrToIsrHandler could be a valid pointer to isr_handler?







c++ metaprogramming c++17






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 6 at 22:12









K. KoovalskyK. Koovalsky

16219




16219












  • This seems like an XY problem. What's the actual problem you're trying to solve?

    – Barry
    Mar 6 at 22:14











  • If you want to call a member function you need an instance to call it on. I don't see one anywhere in your example. Other then that I think you are looking for partial template specialization. You can pass on MemberFunction and decltype(MemberFunction) and use partial specialization to pick up return value and the argument list.

    – super
    Mar 6 at 22:51











  • Might be able to help if you can post the rough syntax for how you would like to use your class. Mainly show how you instantiate enable_member_isr_handler and how you intend to call isr_handler.

    – Always Confused
    Mar 6 at 22:53












  • @Barry of course not, this is a bigger problem, but I always split it into smaller problems and almost every problem I try to solve is an XY problem. I'm just experimenting and I try to see whether it can work or not.

    – K. Koovalsky
    Mar 7 at 6:05











  • @super the question is really about, how to define a function with the same signature as other function. The main problem I have is how to pick the argument list.

    – K. Koovalsky
    Mar 7 at 6:05

















  • This seems like an XY problem. What's the actual problem you're trying to solve?

    – Barry
    Mar 6 at 22:14











  • If you want to call a member function you need an instance to call it on. I don't see one anywhere in your example. Other then that I think you are looking for partial template specialization. You can pass on MemberFunction and decltype(MemberFunction) and use partial specialization to pick up return value and the argument list.

    – super
    Mar 6 at 22:51











  • Might be able to help if you can post the rough syntax for how you would like to use your class. Mainly show how you instantiate enable_member_isr_handler and how you intend to call isr_handler.

    – Always Confused
    Mar 6 at 22:53












  • @Barry of course not, this is a bigger problem, but I always split it into smaller problems and almost every problem I try to solve is an XY problem. I'm just experimenting and I try to see whether it can work or not.

    – K. Koovalsky
    Mar 7 at 6:05











  • @super the question is really about, how to define a function with the same signature as other function. The main problem I have is how to pick the argument list.

    – K. Koovalsky
    Mar 7 at 6:05
















This seems like an XY problem. What's the actual problem you're trying to solve?

– Barry
Mar 6 at 22:14





This seems like an XY problem. What's the actual problem you're trying to solve?

– Barry
Mar 6 at 22:14













If you want to call a member function you need an instance to call it on. I don't see one anywhere in your example. Other then that I think you are looking for partial template specialization. You can pass on MemberFunction and decltype(MemberFunction) and use partial specialization to pick up return value and the argument list.

– super
Mar 6 at 22:51





If you want to call a member function you need an instance to call it on. I don't see one anywhere in your example. Other then that I think you are looking for partial template specialization. You can pass on MemberFunction and decltype(MemberFunction) and use partial specialization to pick up return value and the argument list.

– super
Mar 6 at 22:51













Might be able to help if you can post the rough syntax for how you would like to use your class. Mainly show how you instantiate enable_member_isr_handler and how you intend to call isr_handler.

– Always Confused
Mar 6 at 22:53






Might be able to help if you can post the rough syntax for how you would like to use your class. Mainly show how you instantiate enable_member_isr_handler and how you intend to call isr_handler.

– Always Confused
Mar 6 at 22:53














@Barry of course not, this is a bigger problem, but I always split it into smaller problems and almost every problem I try to solve is an XY problem. I'm just experimenting and I try to see whether it can work or not.

– K. Koovalsky
Mar 7 at 6:05





@Barry of course not, this is a bigger problem, but I always split it into smaller problems and almost every problem I try to solve is an XY problem. I'm just experimenting and I try to see whether it can work or not.

– K. Koovalsky
Mar 7 at 6:05













@super the question is really about, how to define a function with the same signature as other function. The main problem I have is how to pick the argument list.

– K. Koovalsky
Mar 7 at 6:05





@super the question is really about, how to define a function with the same signature as other function. The main problem I have is how to pick the argument list.

– K. Koovalsky
Mar 7 at 6:05












1 Answer
1






active

oldest

votes


















1














As mentioned in my comment, one approach to get the argument list is to use partial specialization and forward the type of the passed in member function. The same approach can be used on free functions as well.



#include <iostream>

template <auto MemberFunction, class MemberFunctionType>
struct handler_impl;

template <auto MemberFunction, class ReturnType, class ClassType, class... Args>
struct handler_impl<MemberFunction, ReturnType(ClassType::*)(Args...)>
// here you have access to return-type, class-type and args

static ReturnType call_on_instance(ClassType& obj, Args... args)
return (obj.*MemberFunction)(args...);

;

template <auto MemberFunction>
using handler = handler_impl<MemberFunction, decltype(MemberFunction)>;

struct Foo
int print(int x)
std::cout << x*x << std::endl;
return x;

;

int main()
Foo f;
handler<&Foo::print>::call_on_instance(f, 7);



One thing to keep in mind here is that we are not using perfect forwarding. The easiest approach is to simply make call_on_instance a template function, then we don't really need to care about the arguments and return value, we can let the compiler deduce it.



Another option is to use the approach I showed above and use a static_assert to make sure the argument list passed in is valid, and thus give a better error message when used in the wrong way.






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%2f55033007%2fgenerate-function-with-signature-of-pointers-type-passed-to-template%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    As mentioned in my comment, one approach to get the argument list is to use partial specialization and forward the type of the passed in member function. The same approach can be used on free functions as well.



    #include <iostream>

    template <auto MemberFunction, class MemberFunctionType>
    struct handler_impl;

    template <auto MemberFunction, class ReturnType, class ClassType, class... Args>
    struct handler_impl<MemberFunction, ReturnType(ClassType::*)(Args...)>
    // here you have access to return-type, class-type and args

    static ReturnType call_on_instance(ClassType& obj, Args... args)
    return (obj.*MemberFunction)(args...);

    ;

    template <auto MemberFunction>
    using handler = handler_impl<MemberFunction, decltype(MemberFunction)>;

    struct Foo
    int print(int x)
    std::cout << x*x << std::endl;
    return x;

    ;

    int main()
    Foo f;
    handler<&Foo::print>::call_on_instance(f, 7);



    One thing to keep in mind here is that we are not using perfect forwarding. The easiest approach is to simply make call_on_instance a template function, then we don't really need to care about the arguments and return value, we can let the compiler deduce it.



    Another option is to use the approach I showed above and use a static_assert to make sure the argument list passed in is valid, and thus give a better error message when used in the wrong way.






    share|improve this answer



























      1














      As mentioned in my comment, one approach to get the argument list is to use partial specialization and forward the type of the passed in member function. The same approach can be used on free functions as well.



      #include <iostream>

      template <auto MemberFunction, class MemberFunctionType>
      struct handler_impl;

      template <auto MemberFunction, class ReturnType, class ClassType, class... Args>
      struct handler_impl<MemberFunction, ReturnType(ClassType::*)(Args...)>
      // here you have access to return-type, class-type and args

      static ReturnType call_on_instance(ClassType& obj, Args... args)
      return (obj.*MemberFunction)(args...);

      ;

      template <auto MemberFunction>
      using handler = handler_impl<MemberFunction, decltype(MemberFunction)>;

      struct Foo
      int print(int x)
      std::cout << x*x << std::endl;
      return x;

      ;

      int main()
      Foo f;
      handler<&Foo::print>::call_on_instance(f, 7);



      One thing to keep in mind here is that we are not using perfect forwarding. The easiest approach is to simply make call_on_instance a template function, then we don't really need to care about the arguments and return value, we can let the compiler deduce it.



      Another option is to use the approach I showed above and use a static_assert to make sure the argument list passed in is valid, and thus give a better error message when used in the wrong way.






      share|improve this answer

























        1












        1








        1







        As mentioned in my comment, one approach to get the argument list is to use partial specialization and forward the type of the passed in member function. The same approach can be used on free functions as well.



        #include <iostream>

        template <auto MemberFunction, class MemberFunctionType>
        struct handler_impl;

        template <auto MemberFunction, class ReturnType, class ClassType, class... Args>
        struct handler_impl<MemberFunction, ReturnType(ClassType::*)(Args...)>
        // here you have access to return-type, class-type and args

        static ReturnType call_on_instance(ClassType& obj, Args... args)
        return (obj.*MemberFunction)(args...);

        ;

        template <auto MemberFunction>
        using handler = handler_impl<MemberFunction, decltype(MemberFunction)>;

        struct Foo
        int print(int x)
        std::cout << x*x << std::endl;
        return x;

        ;

        int main()
        Foo f;
        handler<&Foo::print>::call_on_instance(f, 7);



        One thing to keep in mind here is that we are not using perfect forwarding. The easiest approach is to simply make call_on_instance a template function, then we don't really need to care about the arguments and return value, we can let the compiler deduce it.



        Another option is to use the approach I showed above and use a static_assert to make sure the argument list passed in is valid, and thus give a better error message when used in the wrong way.






        share|improve this answer













        As mentioned in my comment, one approach to get the argument list is to use partial specialization and forward the type of the passed in member function. The same approach can be used on free functions as well.



        #include <iostream>

        template <auto MemberFunction, class MemberFunctionType>
        struct handler_impl;

        template <auto MemberFunction, class ReturnType, class ClassType, class... Args>
        struct handler_impl<MemberFunction, ReturnType(ClassType::*)(Args...)>
        // here you have access to return-type, class-type and args

        static ReturnType call_on_instance(ClassType& obj, Args... args)
        return (obj.*MemberFunction)(args...);

        ;

        template <auto MemberFunction>
        using handler = handler_impl<MemberFunction, decltype(MemberFunction)>;

        struct Foo
        int print(int x)
        std::cout << x*x << std::endl;
        return x;

        ;

        int main()
        Foo f;
        handler<&Foo::print>::call_on_instance(f, 7);



        One thing to keep in mind here is that we are not using perfect forwarding. The easiest approach is to simply make call_on_instance a template function, then we don't really need to care about the arguments and return value, we can let the compiler deduce it.



        Another option is to use the approach I showed above and use a static_assert to make sure the argument list passed in is valid, and thus give a better error message when used in the wrong way.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 7 at 11:20









        supersuper

        3,6302818




        3,6302818





























            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%2f55033007%2fgenerate-function-with-signature-of-pointers-type-passed-to-template%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

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко