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 7028447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:23:16+00:00 2026-05-28T00:23:16+00:00

I would like some way to get the first parameter type of a lambda

  • 0

I would like some way to get the first parameter type of a lambda function, is this possible?

e.g.

instead of:

template<typename T>
struct base
{
     virtual bool operator()(T) = 0;
}

template<typename F, typename T>
struct filter : public base<T>
{
     virtual bool operator()(T) override {return /*...*/ }
};

template<typename T, typename F>
filter<T> make_filter(F func)
{
      return filter<F, T>(std::move(func));
}

auto f = make_filter<int>([](int n){return n % 2 == 0;});

I would like:

template<typename F>
struct filter : public base<typename param1<F>::type>
{
     bool operator()(typename param1<F>::type){return /*...*/ }
};

template<typename F>
filter<F> make_filter(F func)
{
      return filter<F>(std::move(func));
}

auto f = make_filter([](int n){return n % 2 == 0;});

Based on Xeo’s answer this is what I got working in VS2010:

template<typename FPtr>
struct arg1_traits_impl;

template<typename R, typename C, typename A1>
struct arg1_traits_impl<R (C::*)(A1)>{typedef A1 arg1_type;};

template<typename R, typename C, typename A1>
struct arg1_traits_impl<R (C::*)(A1) const>{typedef A1 arg1_type;};

template<typename T>
typename arg1_traits_impl<T>::arg1_type arg1_type_helper(T);

template<typename F>
struct filter : public base<typename std::decay<decltype(detail::arg1_type_helper(&F::operator()))>::type>
{
    bool operator()(typename std::decay<decltype(detail::arg1_type_helper(&F::operator()))>::type){return /*...*/ }
};

template<typename T, typename F>
filter<F> make_filter(F func)
{
      return filter<F>(std::move(func));
}

I’ve tried simplifying the the code, but any attempt seems to break it.

  • 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-28T00:23:16+00:00Added an answer on May 28, 2026 at 12:23 am

    The easiest option would be to just make the operator() a template itself:

    template<typename F>
    struct filter
    {
         template<class Arg>
         void operator(Arg&& arg){
           // use std::forward<Arg>(arg) to call the stored function
         }
    };
    
    template<typename F>
    filter<F> make_filter(F func)
    {
          return filter<F>(std::move(func));
    }
    
    auto f = make_filter([](int n){return n % 2 == 0;});
    

    Now, theoretically, the following code should just work. However, it doesn’t with MSVC10 thanks to a bug:

    #include <iostream>
    #include <typeinfo>
     
    template<class FPtr>
    struct function_traits;
     
    template<class T, class C>
    struct function_traits<T (C::*)>
    {
        typedef T type;
    };
     
    template<class F>
    void bar(F f){
      typedef typename function_traits<
          decltype(&F::operator())>::type signature;
      std::cout << typeid(signature).name();
    }
     
    int main(){
        bar([](int n){ return n % 2 == 0; });
    }
    

    Here‘s an example on how it would look with GCC. MSVC10, however, simply doesn’t compile the code. See this question of mine for further detail. Basically, MSVC10 doesn’t treat decltype(&F::operator()) as a dependent type. Here’s a work-around that was devised in a chat discussion:

    #include <iostream>
    #include <typeinfo>
    #include <type_traits>
    
    template<class FPtr>
    struct function_traits;
    
    template<class R, class C, class A1>
    struct function_traits<R (C::*)(A1)>
    {   // non-const specialization
        typedef A1 arg_type;
        typedef R result_type;
        typedef R type(A1);
    };
    
    template<class R, class C, class A1>
    struct function_traits<R (C::*)(A1) const>
    {   // const specialization
        typedef A1 arg_type;
        typedef R result_type;
        typedef R type(A1);
    };
    
    template<class T>
    typename function_traits<T>::type* bar_helper(T);
    
    template<class F>
    void bar(F f){
      typedef decltype(bar_helper(&F::operator())) fptr;
      typedef typename std::remove_pointer<fptr>::type signature;
      std::cout << typeid(signature).name();
    }
    
    int main(){
        bar([](int n){ return n % 2 == 0; });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know if there is some way to share a variable
I have a String which I would like to modify in some way. For
I would like to add some icons to my treeview. Is there a way
I'm implementing some code generators, i would like to know if there's any way
I'm not sure this is possible, but is there a way, using template programming
I would like to get some help on how to setup paypal payment with
I would like some advice on the best approach to use in the following
I would like some of my preferences to have icons, like the Settings app.
I am developing a site and i would like some simple markup. I would
I am learning C++ exceptions and I would like some clarification of the scenario:

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.