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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:56:40+00:00 2026-06-07T14:56:40+00:00

#include <memory> struct foo { std::unique_ptr<int> p; }; int main() { foo bar {

  • 0
#include <memory>

struct foo
{
    std::unique_ptr<int> p;
};

int main()
{
    foo bar { std::unique_ptr<int>(new int(42)) };
    // okay

    new foo { std::unique_ptr<int>(new int(42)) };
    // error: no matching function for call to
    // 'foo::foo(<brace-enclosed initializer list>)'
}

Does uniform initialization not work with dynamic objects, or is this a shortcoming of g++ 4.6.1?


It works with g++ 4.7.1, but both lines in main fail to compile if foo inherits from another class:

struct baz
{
    // no data members, just some member functions
};

struct foo : baz
{
    std::unique_ptr<int> p;
};

Again, shortcoming of my compiler? Or does uniform initialization not play well with inheritance?

  • 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-07T14:56:43+00:00Added an answer on June 7, 2026 at 2:56 pm

    It builds fine with g++-4.7. So presumably the latter. I’ll have a look to see if I can find stronger evidence via the docs.

    And in response to the inheritance addendum:

    This simpler case also fails to compile:

    struct baz
    {
    };
    
    struct foo : baz
    {
        int b;
    };
    
    int main()
    {
        foo bar { 12 };
    }
    

    With:

    testoo.cpp:14:18: error: no matching function for call to ‘foo::foo(<brace-enclosed initializer list>)’
    testoo.cpp:14:18: note: candidates are:
    testoo.cpp:7:8: note: foo::foo()
    testoo.cpp:7:8: note:   candidate expects 0 arguments, 1 provided
    testoo.cpp:7:8: note: constexpr foo::foo(const foo&)
    testoo.cpp:7:8: note:   no known conversion for argument 1 from ‘int’ to ‘const foo&’
    testoo.cpp:7:8: note: constexpr foo::foo(foo&&)
    testoo.cpp:7:8: note:   no known conversion for argument 1 from ‘int’ to ‘foo&&’
    

    According to my reading of the standard, you have been getting aggregate initialization in your first example:

    An aggregate is an array or a class (Clause 9) with no user-provided
    constructors (12.1), no brace-or-equal- initializers for non-static
    data members (9.2), no private or protected non-static data members
    (Clause 11), no base classes (Clause 10), and no virtual functions
    (10.3).

    When an aggregate is initialized by an initializer list, as specified
    in 8.5.4, the elements of the initializer list are taken as
    initializers for the members of the aggregate, in increasing subscript
    or member order.

    Note that this explicitly forbids base classes. So to sum up – aggregate initialization is not allowed in the presence of base classes. And hence neither of the second examples will compile.

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

Sidebar

Related Questions

Code: #include <memory> using namespace std; struct T {}; T* foo() { return new
// dll #include <memory> __declspec(dllexport) std::auto_ptr<int> get(); __declspec(dllexport) std::auto_ptr<int> get() { return std::auto_ptr<int>(new int());
#include<iostream> using namespace std; struct data { int x; data *ptr; }; int main()
I have a structure named Foo which contains a unique_ptr struct Foo { std::unique_ptr<Bar>
Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct
#include <iostream> #include <tuple> int main(){ auto bt=std::make_tuple(std::tuple<>(),std::tuple<std::tuple<>>()); //Line 1 auto bt2=std::make_tuple(std::tuple<>(),std::tuple<>()); //Line 2
#include <iostream> using namespace std; int main() { int a, b, c, max; cout<<a=;
#include <stdio.h> typedef struct { short x,y; char type; } Tile; int main(int argc,
#include <stdio.h> int main() { printf(%d, sizeof(struct token *)); } The above code can
Consider the following C++ program: #include <memory> struct A {}; struct B : 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.