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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:07:50+00:00 2026-06-15T20:07:50+00:00

Main question I’m trying to compile the following code with GCC 4.7.2: #include <iostream>

  • 0

Main question

I’m trying to compile the following code with GCC 4.7.2:

#include <iostream>

int foo() {
    static int bar;
    return [&bar] () { return bar++; } (); // lambda capturing by reference
}

int main (int argc, char* argv[]) {
    std::cout << foo() << std::endl;
    return 0;
}

And it seems that is not going well, as the output is this one:

$p2.cpp: In function ‘int foo()’:
$p2.cpp:6:14: warning: capture of variable ‘bar’ with non-automatic storage duration [enabled by default]
$p2.cpp:4:16: note: ‘int bar’ declared here

So, my first question would be:

Is this a failure of GCC, or the code is not legit C++11? Is this fixed in any recent version of GCC?

Using the trick in a shared_ptr factory

I consider to build an artifact based on this principle but using a non-literal static variable. This artifact is meant to be a factory of shared_ptr< T > objects, which avoid the creation of new T objects when you just need a duplicate shared_ptr container for the same instance.

This artifact would look like:

std::shared_ptr<Foo> create(std::string name) {
    static std::unordered_map<std::string,std::weak_ptr<Foo>> registry;

    if (auto it = registry.find(name) != registry.end())
        return registry[name].lock();

    auto b = std::shared_ptr<Foo>(
        new Foo(name), 
        [&registry] (Foo* p) {
            registry.erase(p->getName());
            delete p;
        });

    registry.emplace(name,b);
    return b;
}

As far as I know, if the GCC issue discussed before is not a problem in terms of C++11 conformance, this artifact shouldn’t be an issue either. The only thing to take care by using this hack, is to not set the resulting shared_ptr< T > object to any global object which could be destructed after the static variable.

Am I right about this?

  • 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-06-15T20:07:52+00:00Added an answer on June 15, 2026 at 8:07 pm

    Why are you even trying to capture bar? It’s static. You don’t need to capture it at all. Only automatic variables need capturing. Clang throws a hard error on your code, not just a warning. And if you simply remove the &bar from your lambda capture, then the code works perfectly.

    #include <iostream>
    
    int foo() {
        static int bar;
        return [] () { return bar++; } (); // lambda capturing by reference
    }
    
    int main (int argc, char* argv[]) {
        std::cout << foo() << std::endl;
        std::cout << foo() << std::endl;
        std::cout << foo() << std::endl;
        return 0;
    }
    

    prints

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

Sidebar

Related Questions

I'm loading jQuery via Google's CDN using the following code. My main question is
I new in Zend. Main Question: Is this code good for user login (Its
I know how to functionally do each of the following scenarios, my main question
I have the following code : public class Main { public void method(Object o)
Below my code. It returns exception InvalidCastException. And main question is - why? What
Main question is: How to run the code within TestingButton_Click on several threads in
My main question is the following: is information hidding going to hurt perfomance(Both CPU/Memory)
A quick question. I have the following code which gets the mod date of
Main question is how to customize bootstrap responsive css? My code partly works but
When i try use a webservice i get the following exception. My main question

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.