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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:23:29+00:00 2026-06-17T23:23:29+00:00

The following code neither compiles with -std=c++11 under gcc-4.7.1 nor clang-3.2. So I think

  • 0

The following code neither compiles with -std=c++11 under gcc-4.7.1 nor clang-3.2. So I think I did something wrong. But I don’t know why. Can someone give me a hint? Basically if I remove the in-class member initializer for X, it works. So why doesn’t initializer list work with in-class member initializer?

struct X {
    int x = 1;
    int y = 1;
};

int main() {
    X x = {1, 2};
}

gcc compile error:

a.cpp: In function 'int main()':
a.cpp:7:16: error: could not convert '{1, 2}' from '<brace-enclosed initializer list>' to 'X'
  • 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-17T23:23:30+00:00Added an answer on June 17, 2026 at 11:23 pm

    By having the initialization of non-static data members at the point of declaration, your class is no longer an aggregate (see 8.5.1 Aggregates [decl.init.aggr]).

    A workaround is to add a two-parameter constructor. This allows you to use initializer-list initialization, which allows same syntax as aggregate initialization, even if your class is not technically an aggregate.

    struct X {
      X(int x, int y) : x(x), y(y) {}
        int x = 1;
        int y = 1;
    };
    
    int main() 
    {
        X x1{1, 2};
        X x2 = {1,2};
    }
    

    Note: These rules have been relaxed for C++1y, meaning your type would indeed be an aggregate.

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

Sidebar

Related Questions

The following code should display a message but shows me nothing (neither on the
I have the following code alternatives, neither of which is satisfactory. I'm using jQuery
The following code has the disadvantage that the worker thread will neither terminate immediately
What is the correct way to use std::ref ? I tried following code in
I think, the following can't be done in Java. But I would be happy
following code for partition #include<iostream> using namespace std; #define maxn 10000 int x[maxn]; void
I wrote the following code to authenticate a facebook application but instead of google
I had, and have since corrected, something like the following code: class SomeClass {
So, I have the following code which fails to compile on gcc 4.2.1 on
Following is some obviously-defective code for which I think the compiler should emit 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.