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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:17:10+00:00 2026-06-12T02:17:10+00:00

This small test program: #include <functional> //template<class T> // <– with this, gcc compiles

  • 0

This small test program:

#include <functional>

//template<class T>            // <-- with this, gcc compiles  ok
template<class T=void>
struct  c{
        std::function<int(int)> f = [](int i){return i+i;};
};

int main() {};

Clang-3.2 compiles it ok, but from GCC 4.7.1 and 4.8 I am getting strange error:

t.cc:6:31: error: default argument for template parameter for class enclosing ‘struct __lambda0’
  function<int(int)> f = [](int i){return i+i;};
                               ^

Is this one of those obscure C++ rules exceptions that nobody knows about or is it a GCC bug?

EDIT
Looks like a bug. I’ve filed bug report

  • 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-12T02:17:11+00:00Added an answer on June 12, 2026 at 2:17 am

    I think this is a g++ bug with default member initialization. I’m not positive about this, and so have the following supporting evidence:

    template<class T=void>
    struct  c {
       std::function<int(int)> f;
       c() : f([](int i){return i+i;}) {
       }
    };
    
    int main() {}
    

    If that works, what you’re doing should work too. And it does, even if you construct a c.

    Personally, I think default member initialization should be used sparingly and with care. I think it’s really easy to create a lot of confusion with it because most people expect all the initialization to be done in the constructor, and member initializers are not necessarily anywhere near any constructor. So they can leave someone scratching their head wondering how some member gets a particular value.

    I can see cases, especially with simple, mostly-data classes for which it would work pretty well. But mostly, I think if you have a constructor body of any kind, you should probably not be using default member initialization.

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

Sidebar

Related Questions

Here's a small test program I wrote: #include <iostream> using namespace std; class A
I'm running this small C# test program launched from a pre-commit batch file private
Consider this small program: #include <stdio.h> #include <stdlib.h> // Change 60000 to 70000 and
This line works correctly in a small test program, but in the program for
I wrote a small test program with a sample class containing also self-defined constructor,
At first, small program: #include <mysql++.h> using namespace mysqlpp; void mainuu () { Connection
Work on this small test application to learn threading/locking. I have the following code,
This is a pretty esoteric issue that I can't produce a small test case
I have this small class looking like this: private static int field1 = -
I have this small class called City that simply holds some information about a

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.