Executables produced by clang 3.5.0 and gcc 4.9.1 from the code
#include <iostream>
struct Foo
{
Foo() { std::cout << "Foo()" << std::endl; }
Foo(int x) { std::cout << "Foo(int = " << x << ")" << std::endl; }
Foo(int x, int y) { std::cout << "Foo(int = " << x << ", int = " << y << ")" << std::endl; }
};
int main() // Output
{ // ---------------------
auto a = Foo(); // Foo()
auto b = Foo(1); // Foo(int = 1)
auto c = Foo(2, 3); // Foo(int = 2, int = 3)
auto d = Foo{}; // Foo()
auto e = Foo{1}; // Foo(int = 1)
auto f = Foo{2, 3}; // Foo(int = 2, int = 3)
auto g = Foo({}); // Foo(int = 0) <<< Why?
auto h = Foo({1}); // Foo(int = 1)
auto i = Foo({2, 3}); // Foo(int = 2, int = 3)
}
behave as commented.
From cppreference: cpp/language/list initialization:
[...] T( { arg1, arg2, ... } ) (7) [...]
The effects of list initialization of an object of type T are:
If
T
is an aggregate type, aggregate initialization is performed.Otherwise, If the braced-init-list is empty and
T
is a class type with a default constructor, value-initialization is performed.[...]
I concluded that Foo({})
should call the default constructor.
Where's the bug?