Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

418
Views
Is it possilbe to get a pointer to a generic lambda explicit instantiation?

Is it possible to get a (member) function pointer to a specific instantiation of a generic lambda?

I know I can do so for standard non capturing lambdas, and for abbreviated templates, but I can't seem to be able to get a member function pointer for the explicitly instantiated operator() call operator member function of the invented type for the generic lambda.

#include <iostream>
 
void f1( auto v)  { std::cout << v << std::endl; }
int main() {
    void (*pf)(int) = f1<int>; // OK
    void (*pf2)(int) = [](int v)  { std::cout << v << std::endl; } ; // OK
    [](auto v) { std::cout << v << std::endl; }.operator() < int > (42); // OK
    auto generic_template = [](auto v)  { std::cout << v << std::endl; } ;
    using generic_type = decltype (generic_template);
    // void (generic_type::*pf3)(int) = &generic_type::operator()<int>;  // fails to compile

    pf(5);
}

The interest here is academic.

Edit:

As a note of interest to future readers the solutions offered to this question also apply to getting function pointers for lambdas with capture, in addition to generic lambdas. For example, based on the answers :

  auto generic_lambda = [](auto v)  { std::cout << v << std::endl; } ;
  using generic_type = decltype (generic_lambda);
  void (generic_type::*pf1)(int) const = &generic_type::operator();
  (&generic_lambda->*pf1)(43); // OK

  int x = 5;
  auto capturing_lambda = [x](int v)  { std::cout << v+x << std::endl; } ;
  using capturing_type = decltype (capturing_lambda);
  void (capturing_type::*pf2)(int) const = &capturing_type::operator();
  (&capturing_lambda->*pf2)(43); // OK
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Yes, and you can omit the template arguments if they can be deduced from the type being initialized (or the result type of a cast) but since the lambda isn’t mutable the member function is const and so must be the pointer-to-member.

over 4 years ago · Santiago Trujillo Report

0

Is it possible to get a (member) function pointer to a specific instantiation of a generic lambda?

Lambda's operator() is const-qualified by default, you need to add const to the member function pointer type

void (generic_type::*pf3)(int) const = &generic_type::operator();

And since pf3 is a member function pointer, please note that it need a specific lambda object and uses .* or ->* to invoke.

(generic_template.*pf3)(42);

Demo

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!