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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:58:37+00:00 2026-05-27T17:58:37+00:00

This is the simplest example I could come up with that reproduces the problem.

  • 0

This is the simplest example I could come up with that reproduces the problem.

template<class T>
struct X
{
    static void foo()
    {
        static int z = 0;
        []{ z = 1; }();
    }
};

int main()
{
    X<int>::foo();
    return 0;
}

I’ve tried it with MinGW 4.6 and 4.7, also g++ 4.6 in Ubuntu and all of them give me the link error “undefined reference to `z'”. So now that makes me wonder if this is even legal. VC10 has no problem with it.

It works if X is normal class instead of a template. Also, I don’t think it’s related to lambdas cause I get the error even if I replace the lambda with a local class.

  • 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-27T17:58:37+00:00Added an answer on May 27, 2026 at 5:58 pm

    g++ accepts the following, but VC++ does not:

    [&z]{ z = 1; }();
    

    Here z is being captured so g++ does not complain about an undefined reference. However:

    5.1.2/10:

    The identifiers in a capture-list are looked up using the usual rules for unqualified name lookup (3.4.1); each such lookup shall find
    a variable with automatic storage duration declared in the reaching
    scope of the local lambda expression.

    z is not automatic storage. z can therefore not be captured. g++ behavior is therefore incorrect, and VC++ is correct.

    In your code, that VC++ accepts and g++ does not:

    []{ z = 1; }();
    

    z is accessed by VC++ as static storage, which is allowed in a lambda body. g++ apparently does not resolve the name z to the static variable declared above and therefore throws undefined reference, while it shouldn’t.

    tl;dr
    It’s probably a bug in g++

    Edit:
    It is indeed a bug and is fixed in 4.7.

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

Sidebar

Related Questions

This seems like a very simple and a very common problem. The simplest example
I have placed below the simplest example I could come up with to demonstrate
I think this is pretty much the simplest case for mapping a Map (that
I need to reduce this boolean expression to its simplest form. Its given that
I have a custom UIComponent that is basically just this: public class WhiteboardUIComponent extends
template<class T> void swap(T &a, T &b) { T t; t = a; a
Please note this is just an example: <img src=img/normal-font.png onclick=javascript:document.body.style.fontSize = '13px'; /> &nbsp;
For example, there is a interface IMyInterface , and three classes support this interface:
I nearly always bump into this problem in VHDL, and it probably has to
For example, compare these two: function Person(name) { this.name = name; } var john

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.