#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?
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:
With:
According to my reading of the standard, you have been getting
aggregate initializationin your first example: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.