Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5984141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:19:55+00:00 2026-05-22T22:19:55+00:00

How can I detect the return type and parameter types of nullary and unary

  • 0

How can I detect the return type and parameter types of nullary and unary function pointers, std::function objects, and functors (including lambdas)?

Boost’s function_traits and functional traits don’t quite get me there out of the box, but I’m open to supplementing or replacing them.

I could do something like this:

namespace nsDetail
{
    class Dummy { Dummy(); };
}

template<class Fn> struct FnTraits;

template<class R>
struct FnTraits<R(*)()>
{
    typedef nsDetail::Dummy ParamType;
    typedef R               ReturnType;
    typedef R Signature();
};

template<class R, class P>
struct FnTraits<R(*)(P)>
{
    typedef P ParamType;
    typedef R ReturnType;
    typedef R Signature( P );
};

template<class R>
struct FnTraits< std::function<R()> >
{
    typedef nsDetail::Dummy ParamType;
    typedef R               ReturnType;
    typedef R Signature();
};

template<class R, class P>
struct FnTraits< std::function<R(P)> >
{
    typedef P ParamType;
    typedef R ReturnType;
    typedef R Signature( P );
};

But how should I specialize for functors/lambdas?

Update: Perhaps something like in this answer to a different question, but translated from overloading to specialization?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:19:56+00:00Added an answer on May 22, 2026 at 10:19 pm

    It’s not possible in the general case for functors, i.e. class types using operator(). This includes lambda objects, too. Consider a case where operator() is overloaded:

    struct functor {
        double
        operator()(double) const;
    
        int
        operator()(int) const;
    };
    
    typedef function_traits<functor>::result_type result_type;
    

    What should result_type be?

    Note that, as a workaround, some protocols (e.g. boost::apply_visitor from Boost.Variant) require that a result_type be present in the class, with the assumption that all overloads, while accepting different types, all return a type compatible with this result_type.

    And of course given some types T0 ... Tn, std::result_of<functor(T0, ..., Tn)>::type gives the return type associated to the parameter types.


    In the case where exactly one overload of operator() is present[1], you can take the operator() member and inspect that.

    struct not_overloaded {
        double
        operator()(double) const;
    };
    
    template<typename T>
    struct functor_traits {
        typedef decltype(&T::operator()) type;
    };
    

    functor_traits<not_overloaded>::type has type double (not_overloaded::*)(double) const here, and with just a bit of effort you can extract from this what you want. (e.g. a specialization of the form Ret (T::*)(Args...) const will match that type.)

    [1]: but a functor can provide functionality by implicitly converting to a function pointer/reference, too, so you could miss that

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I check if the return type of a function is IEnumerable<T> ?
I need a library that can detect objects in an image (uses edge detection).
A there any g++ options which can detect improper initialization of std::string with NULL
Is there any way that I can detect browser type on routing and use
Is there a Regular Expression that can detect SQL in a string? Does anyone
Is there any way I can detect when my page has been set as
I'm trying to determine how I can detect when the user changes the Windows
I need to know how I can detect the current application pool I am
Is there a way ActionScript 3 can detect the user's screen resolution?
Is there anyway that we can detect when the system/screen gets locked and notify

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.