Simplest way to determine return type of function












42














Given a very simple, but lengthy function, such as:



int foo(int a, int b, int c, int d) {
return 1;
}

// using ReturnTypeOfFoo = ???


What is the most simple and concise way to determine the function's return type (ReturnTypeOfFoo, in this example: int) at compile time without repeating the function's parameter types (by name only, since it is known that the function does not have any additional overloads)?










share|improve this question
























  • Depending on the compiler or the support libraries available to you... docs.microsoft.com/en-us/cpp/cpp/attributes?view=vs-2017, Fairly sure that Concepts also has some APIs which allow you to deduce this information at run time or compile time.
    – Jay
    Dec 7 at 16:33








  • 1




    Maybe decltype(foo)::result_type?
    – Thomas Lang
    Dec 7 at 16:33










  • maybe en.cppreference.com/w/cpp/types/result_of?
    – bracco23
    Dec 7 at 16:35






  • 1




    @ThomasLang where does this come from? decltype(foo) is a function that has no result_type member, or do I miss something?
    – user463035818
    Dec 7 at 16:39










  • @user463035818 You may be right here, I was referring to the result_type member of a std::function type.
    – Thomas Lang
    Dec 7 at 16:40
















42














Given a very simple, but lengthy function, such as:



int foo(int a, int b, int c, int d) {
return 1;
}

// using ReturnTypeOfFoo = ???


What is the most simple and concise way to determine the function's return type (ReturnTypeOfFoo, in this example: int) at compile time without repeating the function's parameter types (by name only, since it is known that the function does not have any additional overloads)?










share|improve this question
























  • Depending on the compiler or the support libraries available to you... docs.microsoft.com/en-us/cpp/cpp/attributes?view=vs-2017, Fairly sure that Concepts also has some APIs which allow you to deduce this information at run time or compile time.
    – Jay
    Dec 7 at 16:33








  • 1




    Maybe decltype(foo)::result_type?
    – Thomas Lang
    Dec 7 at 16:33










  • maybe en.cppreference.com/w/cpp/types/result_of?
    – bracco23
    Dec 7 at 16:35






  • 1




    @ThomasLang where does this come from? decltype(foo) is a function that has no result_type member, or do I miss something?
    – user463035818
    Dec 7 at 16:39










  • @user463035818 You may be right here, I was referring to the result_type member of a std::function type.
    – Thomas Lang
    Dec 7 at 16:40














42












42








42


10





Given a very simple, but lengthy function, such as:



int foo(int a, int b, int c, int d) {
return 1;
}

// using ReturnTypeOfFoo = ???


What is the most simple and concise way to determine the function's return type (ReturnTypeOfFoo, in this example: int) at compile time without repeating the function's parameter types (by name only, since it is known that the function does not have any additional overloads)?










share|improve this question















Given a very simple, but lengthy function, such as:



int foo(int a, int b, int c, int d) {
return 1;
}

// using ReturnTypeOfFoo = ???


What is the most simple and concise way to determine the function's return type (ReturnTypeOfFoo, in this example: int) at compile time without repeating the function's parameter types (by name only, since it is known that the function does not have any additional overloads)?







c++ function c++17 return-type compile-time






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 7 at 17:44









Konrad Rudolph

393k1017771022




393k1017771022










asked Dec 7 at 16:29









Cybran

975717




975717












  • Depending on the compiler or the support libraries available to you... docs.microsoft.com/en-us/cpp/cpp/attributes?view=vs-2017, Fairly sure that Concepts also has some APIs which allow you to deduce this information at run time or compile time.
    – Jay
    Dec 7 at 16:33








  • 1




    Maybe decltype(foo)::result_type?
    – Thomas Lang
    Dec 7 at 16:33










  • maybe en.cppreference.com/w/cpp/types/result_of?
    – bracco23
    Dec 7 at 16:35






  • 1




    @ThomasLang where does this come from? decltype(foo) is a function that has no result_type member, or do I miss something?
    – user463035818
    Dec 7 at 16:39










  • @user463035818 You may be right here, I was referring to the result_type member of a std::function type.
    – Thomas Lang
    Dec 7 at 16:40


















  • Depending on the compiler or the support libraries available to you... docs.microsoft.com/en-us/cpp/cpp/attributes?view=vs-2017, Fairly sure that Concepts also has some APIs which allow you to deduce this information at run time or compile time.
    – Jay
    Dec 7 at 16:33








  • 1




    Maybe decltype(foo)::result_type?
    – Thomas Lang
    Dec 7 at 16:33










  • maybe en.cppreference.com/w/cpp/types/result_of?
    – bracco23
    Dec 7 at 16:35






  • 1




    @ThomasLang where does this come from? decltype(foo) is a function that has no result_type member, or do I miss something?
    – user463035818
    Dec 7 at 16:39










  • @user463035818 You may be right here, I was referring to the result_type member of a std::function type.
    – Thomas Lang
    Dec 7 at 16:40
















Depending on the compiler or the support libraries available to you... docs.microsoft.com/en-us/cpp/cpp/attributes?view=vs-2017, Fairly sure that Concepts also has some APIs which allow you to deduce this information at run time or compile time.
– Jay
Dec 7 at 16:33






Depending on the compiler or the support libraries available to you... docs.microsoft.com/en-us/cpp/cpp/attributes?view=vs-2017, Fairly sure that Concepts also has some APIs which allow you to deduce this information at run time or compile time.
– Jay
Dec 7 at 16:33






1




1




Maybe decltype(foo)::result_type?
– Thomas Lang
Dec 7 at 16:33




Maybe decltype(foo)::result_type?
– Thomas Lang
Dec 7 at 16:33












maybe en.cppreference.com/w/cpp/types/result_of?
– bracco23
Dec 7 at 16:35




maybe en.cppreference.com/w/cpp/types/result_of?
– bracco23
Dec 7 at 16:35




1




1




@ThomasLang where does this come from? decltype(foo) is a function that has no result_type member, or do I miss something?
– user463035818
Dec 7 at 16:39




@ThomasLang where does this come from? decltype(foo) is a function that has no result_type member, or do I miss something?
– user463035818
Dec 7 at 16:39












@user463035818 You may be right here, I was referring to the result_type member of a std::function type.
– Thomas Lang
Dec 7 at 16:40




@user463035818 You may be right here, I was referring to the result_type member of a std::function type.
– Thomas Lang
Dec 7 at 16:40












3 Answers
3






active

oldest

votes


















49














You can leverage std::function here which will give you a typedef for the functions return type. This does require C++17 support, since it relies on class template argument deduction, but it will work with any callable type:



using ReturnTypeOfFoo = decltype(std::function{foo})::result_type;




We can make this a little more generic like



template<typename Callable>
using return_type_of_t =
typename decltype(std::function{std::declval<Callable>()})::result_type;


which then lets you use it like



int foo(int a, int b, int c, int d) {
return 1;
}

auto bar = (){ return 1; };

struct baz_
{
double operator()(){ return 0; }
} baz;

using ReturnTypeOfFoo = return_type_of_t<decltype(foo)>;
using ReturnTypeOfBar = return_type_of_t<decltype(bar)>;
using ReturnTypeOfBaz = return_type_of_t<decltype(baz)>;





share|improve this answer























  • Very compact! I can confirm the one-liner also works for template methods, as long as all template arguments specified.
    – Cybran
    Dec 7 at 19:08



















20














Most simple and concise is probably:



template <typename R, typename... Args>
R return_type_of(R(*)(Args...));

using ReturnTypeOfFoo = decltype(return_type_of(foo));


Note that this won't work for function objects or pointers to member functions. Just functions, that aren't overloaded or templates, or noexcept.



But this can be extended to support all of those cases, if so desired, by adding more overloads of return_type_of.






share|improve this answer





























    14














    I don't know if is the simplest way (if you can use C++17 surely isn't: see NathanOliver's answer) but... what about declaring a function as follows:



    template <typename R, typename ... Args>
    R getRetType (R(*)(Args...));


    and using decltype()?



    using ReturnTypeOfFoo = decltype( getRetType(&foo) );


    Observe that getRetType() is only declared and not defined because is called only a decltype(), so only the returned type is relevant.






    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%2f53673442%2fsimplest-way-to-determine-return-type-of-function%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









      49














      You can leverage std::function here which will give you a typedef for the functions return type. This does require C++17 support, since it relies on class template argument deduction, but it will work with any callable type:



      using ReturnTypeOfFoo = decltype(std::function{foo})::result_type;




      We can make this a little more generic like



      template<typename Callable>
      using return_type_of_t =
      typename decltype(std::function{std::declval<Callable>()})::result_type;


      which then lets you use it like



      int foo(int a, int b, int c, int d) {
      return 1;
      }

      auto bar = (){ return 1; };

      struct baz_
      {
      double operator()(){ return 0; }
      } baz;

      using ReturnTypeOfFoo = return_type_of_t<decltype(foo)>;
      using ReturnTypeOfBar = return_type_of_t<decltype(bar)>;
      using ReturnTypeOfBaz = return_type_of_t<decltype(baz)>;





      share|improve this answer























      • Very compact! I can confirm the one-liner also works for template methods, as long as all template arguments specified.
        – Cybran
        Dec 7 at 19:08
















      49














      You can leverage std::function here which will give you a typedef for the functions return type. This does require C++17 support, since it relies on class template argument deduction, but it will work with any callable type:



      using ReturnTypeOfFoo = decltype(std::function{foo})::result_type;




      We can make this a little more generic like



      template<typename Callable>
      using return_type_of_t =
      typename decltype(std::function{std::declval<Callable>()})::result_type;


      which then lets you use it like



      int foo(int a, int b, int c, int d) {
      return 1;
      }

      auto bar = (){ return 1; };

      struct baz_
      {
      double operator()(){ return 0; }
      } baz;

      using ReturnTypeOfFoo = return_type_of_t<decltype(foo)>;
      using ReturnTypeOfBar = return_type_of_t<decltype(bar)>;
      using ReturnTypeOfBaz = return_type_of_t<decltype(baz)>;





      share|improve this answer























      • Very compact! I can confirm the one-liner also works for template methods, as long as all template arguments specified.
        – Cybran
        Dec 7 at 19:08














      49












      49








      49






      You can leverage std::function here which will give you a typedef for the functions return type. This does require C++17 support, since it relies on class template argument deduction, but it will work with any callable type:



      using ReturnTypeOfFoo = decltype(std::function{foo})::result_type;




      We can make this a little more generic like



      template<typename Callable>
      using return_type_of_t =
      typename decltype(std::function{std::declval<Callable>()})::result_type;


      which then lets you use it like



      int foo(int a, int b, int c, int d) {
      return 1;
      }

      auto bar = (){ return 1; };

      struct baz_
      {
      double operator()(){ return 0; }
      } baz;

      using ReturnTypeOfFoo = return_type_of_t<decltype(foo)>;
      using ReturnTypeOfBar = return_type_of_t<decltype(bar)>;
      using ReturnTypeOfBaz = return_type_of_t<decltype(baz)>;





      share|improve this answer














      You can leverage std::function here which will give you a typedef for the functions return type. This does require C++17 support, since it relies on class template argument deduction, but it will work with any callable type:



      using ReturnTypeOfFoo = decltype(std::function{foo})::result_type;




      We can make this a little more generic like



      template<typename Callable>
      using return_type_of_t =
      typename decltype(std::function{std::declval<Callable>()})::result_type;


      which then lets you use it like



      int foo(int a, int b, int c, int d) {
      return 1;
      }

      auto bar = (){ return 1; };

      struct baz_
      {
      double operator()(){ return 0; }
      } baz;

      using ReturnTypeOfFoo = return_type_of_t<decltype(foo)>;
      using ReturnTypeOfBar = return_type_of_t<decltype(bar)>;
      using ReturnTypeOfBaz = return_type_of_t<decltype(baz)>;






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Dec 8 at 8:50









      coldspeed

      118k18109185




      118k18109185










      answered Dec 7 at 16:44









      NathanOliver

      86.6k15120180




      86.6k15120180












      • Very compact! I can confirm the one-liner also works for template methods, as long as all template arguments specified.
        – Cybran
        Dec 7 at 19:08


















      • Very compact! I can confirm the one-liner also works for template methods, as long as all template arguments specified.
        – Cybran
        Dec 7 at 19:08
















      Very compact! I can confirm the one-liner also works for template methods, as long as all template arguments specified.
      – Cybran
      Dec 7 at 19:08




      Very compact! I can confirm the one-liner also works for template methods, as long as all template arguments specified.
      – Cybran
      Dec 7 at 19:08













      20














      Most simple and concise is probably:



      template <typename R, typename... Args>
      R return_type_of(R(*)(Args...));

      using ReturnTypeOfFoo = decltype(return_type_of(foo));


      Note that this won't work for function objects or pointers to member functions. Just functions, that aren't overloaded or templates, or noexcept.



      But this can be extended to support all of those cases, if so desired, by adding more overloads of return_type_of.






      share|improve this answer


























        20














        Most simple and concise is probably:



        template <typename R, typename... Args>
        R return_type_of(R(*)(Args...));

        using ReturnTypeOfFoo = decltype(return_type_of(foo));


        Note that this won't work for function objects or pointers to member functions. Just functions, that aren't overloaded or templates, or noexcept.



        But this can be extended to support all of those cases, if so desired, by adding more overloads of return_type_of.






        share|improve this answer
























          20












          20








          20






          Most simple and concise is probably:



          template <typename R, typename... Args>
          R return_type_of(R(*)(Args...));

          using ReturnTypeOfFoo = decltype(return_type_of(foo));


          Note that this won't work for function objects or pointers to member functions. Just functions, that aren't overloaded or templates, or noexcept.



          But this can be extended to support all of those cases, if so desired, by adding more overloads of return_type_of.






          share|improve this answer












          Most simple and concise is probably:



          template <typename R, typename... Args>
          R return_type_of(R(*)(Args...));

          using ReturnTypeOfFoo = decltype(return_type_of(foo));


          Note that this won't work for function objects or pointers to member functions. Just functions, that aren't overloaded or templates, or noexcept.



          But this can be extended to support all of those cases, if so desired, by adding more overloads of return_type_of.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 7 at 16:34









          Barry

          176k18304558




          176k18304558























              14














              I don't know if is the simplest way (if you can use C++17 surely isn't: see NathanOliver's answer) but... what about declaring a function as follows:



              template <typename R, typename ... Args>
              R getRetType (R(*)(Args...));


              and using decltype()?



              using ReturnTypeOfFoo = decltype( getRetType(&foo) );


              Observe that getRetType() is only declared and not defined because is called only a decltype(), so only the returned type is relevant.






              share|improve this answer




























                14














                I don't know if is the simplest way (if you can use C++17 surely isn't: see NathanOliver's answer) but... what about declaring a function as follows:



                template <typename R, typename ... Args>
                R getRetType (R(*)(Args...));


                and using decltype()?



                using ReturnTypeOfFoo = decltype( getRetType(&foo) );


                Observe that getRetType() is only declared and not defined because is called only a decltype(), so only the returned type is relevant.






                share|improve this answer


























                  14












                  14








                  14






                  I don't know if is the simplest way (if you can use C++17 surely isn't: see NathanOliver's answer) but... what about declaring a function as follows:



                  template <typename R, typename ... Args>
                  R getRetType (R(*)(Args...));


                  and using decltype()?



                  using ReturnTypeOfFoo = decltype( getRetType(&foo) );


                  Observe that getRetType() is only declared and not defined because is called only a decltype(), so only the returned type is relevant.






                  share|improve this answer














                  I don't know if is the simplest way (if you can use C++17 surely isn't: see NathanOliver's answer) but... what about declaring a function as follows:



                  template <typename R, typename ... Args>
                  R getRetType (R(*)(Args...));


                  and using decltype()?



                  using ReturnTypeOfFoo = decltype( getRetType(&foo) );


                  Observe that getRetType() is only declared and not defined because is called only a decltype(), so only the returned type is relevant.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 7 at 17:26









                  NathanOliver

                  86.6k15120180




                  86.6k15120180










                  answered Dec 7 at 16:34









                  max66

                  34.2k63762




                  34.2k63762






























                      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%2f53673442%2fsimplest-way-to-determine-return-type-of-function%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

                      Quarter-circle Tiles

                      build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

                      Mont Emei