Reputation: 16735
I have confusing situation with simple code:
struct Item {
size_t span{};
};
int main() {
Item item{1}; // error is here
return 0;
}
While compiling this I have following error:
test.cpp: In function ‘int main()’:
test.cpp:8:13: error: no matching function for call to ‘Item::Item(<brace-enclosed initializer list>)’
Item i{1};
^
test.cpp:8:13: note: candidates are:
test.cpp:3:8: note: constexpr Item::Item()
struct Item {
^
test.cpp:3:8: note: candidate expects 0 arguments, 1 provided
test.cpp:3:8: note: constexpr Item::Item(const Item&)
test.cpp:3:8: note: no known conversion for argument 1 from ‘int’ to ‘const Item&’
test.cpp:3:8: note: constexpr Item::Item(Item&&)
test.cpp:3:8: note: no known conversion for argument 1 from ‘int’ to ‘Item&&’
Why g++
tries to find a ctor
for initializer list
in this case instead of simple C-style structure object creating?
If I remove {}
from size_t span{}
it compiles successfully.
It also happens if I change the line to size_t span = 0
so it seems to be some initialization in declaration issue which exists since c++11.
Upvotes: 2
Views: 205
Reputation: 171127
Usign Item item{1};
means you're doing list-initialisation (of item
). List initialisation is defined as follows:
Your class has no constructors. It is also not a (C++11) aggregate, because it contains an initialiser for a non-static data member.
Note that this restriction (member initialisers) was lifted in C++14, so Item
is a C++14 aggregate and your code, while not valid C++11, is valid C++14.
Upvotes: 5