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

  • Home
  • SEARCH
  • 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 6201981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:39:02+00:00 2026-05-24T04:39:02+00:00

I was thrilled when lambda expressions (LE) were part of the gcc starting a

  • 0

I was thrilled when lambda expressions (LE) were part of the gcc starting a 4.5.1 and hoped they would grant a way of getting rid of those nasty functions pointer in C++, which were basically, to my understanding, compiled as C functions. All those static declarations etc…

Now I wanted to use LEs in a class, where one can choose a method of computation by a functor. But due to the definition in the proposal for C++1x, this seems not to be possible at all. Here the code and the problem(s).

testLE.h

#include<functional>
typedef std::function<double(double, double)> tMyOp;
class testLE
{
  public:
  testLE(){ m_oFactor = 2.5; }
  void setOp(const int i)
  {
    if (i > 0) {myOp = plus;} else {myOp = minus;}
  }
  double eval(double x, double y) { return myOp(x, y); }

private:
  double m_oFactor;
  tMyOp plus;
  tMyOp minus;
  tMyOp myOp;
};

testLE.cpp

#include "testLE.h

tMyOp testLE::plus = [](double x, double y) -> double
{
  return m_oFactor*(x + y);
};

tMyOp testLE::minus = [](double x, double y) -> double
{
  return m_oFactor*(x - y);
};

So the problem is, that this will not compile unless I declare the functors _myOp, _minus and _plus as static, but as soon as I do this, I have no access any longer to the member variables (in this case factor). And using [this] instead of [] in the functors’ definition does not work either.

Honestly, imho this is worse than the function pointer alternative…. So I would be very glad about help, but reading the specs for LEs in the new standard does not give much hope.

Thanks and best wishes,
Andy

  • 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-24T04:39:02+00:00Added an answer on May 24, 2026 at 4:39 am

    I find it not entirely clear what you want to do.

    Would defining setOp like this help?

    void testLE::setOp(int i)
    {
        if (i > 0) 
            myOp = [this](double x, double y) -> double { return m_oFactor*(x + y); };
        else
            myOp = [this](double x, double y) -> double { return m_oFactor*(x - y); };
    }
    

    Or you can assign plus and minus in the constructor:

    testLE()::testLE()
    {
        m_oFactor = 2.5;
        plus = [this](double x, double y) -> double { return m_oFactor*(x + y); };
        minus = [this](double x, double y) -> double { return m_oFactor*(x - y); };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm really thrilled, because my code works great for me (se below), however I
Overview As a Swing developer of ten years, I've been thrilled with the features
If my application runs out of memory, I would like to re-run it with
I'm thrilled. Why does this works fine: char ptr[] = hello world!; ptr[0] =
I was thrilled to see the new System.Collections.Concurrent namespace in .Net 4.0, quite nice!
I would like to achieve something I can easily do in .net. What I
OK, so I'm thrilled with Solr , but I can't seem to figure out
Is there currently any data in a javascript mouse event that would allow me
I have used SharePoint's export/import tool in SharePoint 2007... Wasn't too thrilled with it.
Basically I would like to have some dictionary that is an abstaction over legacy

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.