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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:30:25+00:00 2026-05-31T13:30:25+00:00

Is it possible to constrain the type of capture of a lambda given as

  • 0

Is it possible to constrain the type of capture of a lambda given as parameter ?
For example, Is it possible to take only lambdas that don’t capture anything by reference ?

template <typename F>
void f(const F& lambda) // F must be a lambda that do not capture by ref
{
  :::
}
  • 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-31T13:30:26+00:00Added an answer on May 31, 2026 at 1:30 pm

    MSalters notes that “non-capturing lambda’s can be converted to a pointer-to-function.” What does this mean? The lambda object will match a pointer to function parameter type.

    It’s tricky to translate the lambda type to a pointer-to-function. Here is my attempt at a compliant implementation. It’s slightly hackish.

    #include <type_traits>
    
    template< typename fn >
    struct ptmf_to_pf;
    
    template< typename r, typename c, typename ... a >
    struct ptmf_to_pf< r (c::*) ( a ... ) const >
        { typedef r (* type)( a ... ); };
    
    // Use SFINAE to hide function if lambda is not convertible to function ptr.
    // Only check that the conversion is legal, it never actually occurs.
    
    template< typename lambda >
    typename std::enable_if< std::is_constructible<
             typename ptmf_to_pf< decltype( &lambda::operator() ) >::type,
             lambda >::value >::type
    f( lambda arg ) {
        arg( "hello " );
        arg( "world\n" );
    }
    
    #include <iostream>
    
    int main() {
    int x = 3;
        f( []( char const *s ){ std::cout << s; } ); // OK
        f( [=]( char const *s ){ std::cout << s; } ); // OK
        f( [=]( char const *s ){ std::cout << s << x; } ); // error
    }
    

    This will not accept function pointers as direct arguments, since the template parameter needs to resolve to a functor. You could make it do so by providing a specialization for ptmf_to_pf that accepts pointer to function types.

    Also, as the demo shows, it won’t accept lambdas that capture anything by value, as well as by reference. There is no way in C++ to make the restriction so specific.

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

Sidebar

Related Questions

Is it possible to have a type constraint that is an or of two
Is it possible to constraint a method so that it receives only int ,
Is it possible to constrain the type of generic to, say, two different classes?
Given this example: abstract class Base { type Value } case object Foo extends
Is it possible in Windows Forms to constrain input to is data type. For
I have a generic class that I need to constrain to only value types
I can't find anything discussing the case where there are multiple possible indexes that
Is it possible to specify both upper and lower bound constraints on type parameters
Is possible to calculate average of three encrypted integer? No constrain on the method
Possible Duplicate: C# generic constraint for only integers Greets! I'm attempting to set up

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.