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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:59:50+00:00 2026-05-26T22:59:50+00:00

Previous investigation in Can I make a table of String + lambdas that have

  • 0

Previous investigation in Can I make a table of String + lambdas that have the same signature? showed me that I can in fact have a table of strings + lambdas in VS2010.

Things were looking good while the lambdas were void return type. But having tried to change them to bool return type, the compiler seems to get it wrong, or there’s some sort of a memory corruption error… something’s not right in C++ land…

The following illustrates the scenario:

// fun: use a table of lambdas to define what to do in order to update each field
typedef std::function<bool (CDynamicMenuItem *, ITEM *)> LambdaType;
struct UpdateField {
    const TCHAR *   label;
    LambdaType      lambda; // this version allows us to use captures in our lambdas, whereas the following doesn't
    //void (*lambda)(CDynamicMenuItem *, ITEM *);   // this would work in VS11, but the conversion of lambda to function pointer was defined after 2010's release!
};
UpdateField MenuFields[] = {
    { "Identity",   [] (CDynamicMenuItem * pMenuItem, ITEM * pNearestItem) ->bool { return pMenuItem->SetText(FString("%s/%.1f", pNearestItem->thissec->name, pNearestItem->number/10.0)), true; } },
    { "X1",         [] (CDynamicMenuItem * pMenuItem, ITEM * pNearestItem) ->bool { double v = GetX1(pNearestItem); return (v != v) ? false : pMenuItem->SetValue(v), true; } },
    { "Y1",         [] (CDynamicMenuItem * pMenuItem, ITEM * pNearestItem) ->bool { double v = GetY1(pNearestItem); if (v != v) return false; pMenuItem->SetValue(v); return true; } },
    { "X2",         [] (CDynamicMenuItem * pMenuItem, ITEM * pNearestItem) ->bool { double v = GetX2(pNearestItem); if (v != v) return false; pMenuItem->SetValue(v); return true; } },
    { "Y2",         [] (CDynamicMenuItem * pMenuItem, ITEM * pNearestItem) ->bool { double v = GetY2(pNearestItem); if (v != v) return false; pMenuItem->SetValue(v); return true; } },
    { "Xd",         [] (CDynamicMenuItem * pMenuItem, ITEM * pNearestItem) ->bool { double v = GetXd(pNearestItem); if (v != v) return false; pMenuItem->SetValue(v); return true; } },
    { "Yd",         [] (CDynamicMenuItem * pMenuItem, ITEM * pNearestItem) ->bool { double v = GetYd(pNearestItem); if (v != v) return false; pMenuItem->SetValue(v); return true; } },
    { "Angle",      [] (CDynamicMenuItem * pMenuItem, ITEM * pNearestItem) ->bool { double v = GetAngle(pNearestItem); if (v != v) return false; pMenuItem->SetValue(v); return true; } },
    { "Length",     [] (CDynamicMenuItem * pMenuItem, ITEM * pNearestItem) ->bool { double v = GetLength(pNearestItem); if (v != v) return false; pMenuItem->SetValue(v); return true; } },
};

for (UpdateField * it = &MenuFields[0], * end = (MenuFields + countof(MenuFields)); it != end; ++it)
{
    CDynamicMenuItem * pMenuItem = pMenu->FindItem(it->label);
    if (pMenuItem)
    {
        if (!m_pNearestItem || !it->lambda(pMenuItem, m_pNearestItem))
            pMenuItem->SetText("");
    }
}

The above worked beautifully when the lambda’s return type is void (i.e. ->bool is omitted, and the various lambda bodies are modified to not return anything, etc.).

However, it’s useful to me to have them return a bool indicating whether or not the lambda was able to process data for that field, and if not, to have the caller handle clearing that field.

To be certain, the code compiles & runs… until it hits this code & CRASHES. Looking at “MenuFields[]” in the debugger shows garbage for most of the MenuField[x].label addresses (sometimes one of them is correct, but I haven’t figured out what the pattern is).

I thought maybe the compiler was glitching over the syntax of the lambda embedded in the static initialization list, but I’m not sure what I can do about that?

I tried this variation:

    { "Identity",   LambdaType( [] (CDynamicMenuItem * pMenuItem, ITEM * pNearestItem) ->bool { return pMenuItem->SetText(FString("%s/%.1f", pNearestItem->thissec->name, pNearestItem->number/10.0)), true; } ) },

The compiler likes this equally well, but it results in the same corrupted table data.

Similarly, putting parenthesis around the entire lambda is okay by the compiler, and equally corrupt at runtime.

So, some questions:

  1. Do you see something I overlooked?
  2. Can you think of a work around to get the compiler to generate the correct code (other than going back to void return – which is plausible for my scenario, and my likely next step barring a better suggestion)?
  3. Know how best to report this to Microsoft? [I have had no luck finding actual human beings on the other end to give this sort of detailed info to so that it actually goes somewhere other than >nul)
  • 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-26T22:59:51+00:00Added an answer on May 26, 2026 at 10:59 pm

    This is indeed a bug in the Visual C++ compiler. See the bug report “miscompilation of aggregate initializer with lambdas inside”.

    As a workaround, consider not using the aggregate initialization here. Instead, you could use a std::vector:

    UpdateField MakeUpdateField(char const* label, LambdaType const lambda)
    {
        UpdateField f = { label, lambda };
        return f;
    }
    
    std::vector<UpdateField> fields = ([]() -> std::vector<UpdateField>
    {
        std::vector<UpdateField> data;
        data.push_back(MakeUpdateField("Identity", [] { /* contents omitted */ }));
        data.push_back(MakeUpdateField("X1",       [] { /* contents omitted */ }));
        data.push_back(MakeUpdateField("Y1",       [] { /* contents omitted */ }));
        return data;
    })();
    

    (If your data is a mapping of names to lambdas, you might also consider using a std::map, which might provide better performance characteristics or might be easier to use.)

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

Sidebar

Related Questions

A previous question showed a nice way of printing to a string. The answer
My previous question on the same theme: C#: Asynchronous NamedPipeServerStream understanding Now I have
In previous versions of Excel there was a registry entry that you could create
From previous post, I learnt that for there are two ways, at least, to
From previous experience I had been under the impression that it's perfectly legal (though
In previous question of mine, someone had meantioned that using Semaphores were expensive in
Previous questions have asked if it is possible to turn compiled delegates into expression
My previous for the same problem , Now, I am in a same situation
Suppose I have a custom collection class that provides some internal thread synchronization. For
[Previous essay-title for question] Oracle SQL: update parent table column if all child table

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.