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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:23:40+00:00 2026-06-06T02:23:40+00:00

I was trying out some stuff allocating structs, which contain non-pod members, on the

  • 0

I was trying out some stuff allocating structs, which contain non-pod members, on the heap and initializing them using initializer lists. However, the compiler encountered an error on my code. This snippet reproduces it:

#include <vector>

struct A {
    int a;
};

struct B {
    int a;
    std::vector<int> b;
};

int main() {
    std::vector<int> some_vec;
    A a = {1}; // OK
    A b = A{1}; // OK
    A *c = new A{1}; // OK(leaks, NP)
    B d = {1, some_vec}; // OK
    B e = B{1, some_vec}; // OK

    B *f = new B{1, some_vec}; // Fails to compile

    B *g = new B({1, some_vec}); // OK
}

(I know that leaks, I’m aware of that, it’s just a test snippet)

The line pointed out fails to compile, on GCC 4.6.3, with this error:

test.cpp: In function ‘int main()’:
test.cpp:19:29: error: no matching function for call to ‘B::B(<brace-enclosed initializer list>)’
test.cpp:19:29: note: candidates are:
test.cpp:7:8: note: B::B()
test.cpp:7:8: note:   candidate expects 0 arguments, 2 provided
test.cpp:7:8: note: B::B(const B&)
test.cpp:7:8: note:   candidate expects 1 argument, 2 provided
test.cpp:7:8: note: B::B(B&&)
test.cpp:7:8: note:   candidate expects 1 argument, 2 provided

Apparently the compiler can’t initialize my struct using the provided initializer list. The strange this is that the next line after the one that produces an error, which(at far as I can see) just copies(probably moves) a B from another that was constructed using that same initializer list, does not produce any error.

Is there anything wrong I am doing? I mean, I can live using that last line on the snippet provided, but is there any reason why I can’t just create a struct using operator new and an initialization list?

  • 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-06T02:23:41+00:00Added an answer on June 6, 2026 at 2:23 am

    The code should compile and work. I tested it with gcc 4.7.0, and it works fine, so the bug has been fixed it seems. If you read 8.5.4 List-initialization in the standard, they say List intialization can be used as the initializer in a new expression (5.3.4).

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

Sidebar

Related Questions

I am trying to test out some Dapper stuff using LinqPad. Dapper needs a
When an exe file is run it prints out some stuff. I'm trying to
I'm currently trying out the Django framework and I would share/present/show some stuff I've
I'm trying some stuff out in Android and I'm stuck at when trying to
I have traced some ajax stuff and I am trying to figure out what
For trying out some pointer stuff, I made an instance variable in an existing
I am trying to work on some stuff using the Scanner for input, but
I'm new to the Zend Framework and trying to figure some stuff out. I
I am trying out some examples with analytical functions and i have created a
I'm trying out some bean validation and I'm stumbling upon 'strange' behavior. I'm working

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.