Skip to content

Instantly share code, notes, and snippets.

View s9w's full-sized avatar
🔥

Sebastian Werhausen s9w

🔥
View GitHub Profile
@s9w
s9w / gist:ad9b1dd1ea6fb17e956559c8b352e246
Last active June 22, 2024 13:35
Potential issue with C++20's initialization change

Potential issue with C++20's initialization change

C++20 takes yet another swing at its infamous initialization rules. The players involved this time are Aggregate initialization (type a{1, 2, 3}) and direct initialization (type a(1, 2, 3)). A common pitfall with aggregate init is:

std::vector<int> vec0(5, 9); // 9, 9, 9, 9, 9
std::vector<int> vec1{5, 9}; // 5, 9

So if you don't know what you're doing, {} is potentially dangerous to use with types that might have both "real" constructors and such with std::initializer_list. If you had your head in the sand for 10 11 years and always used () then you never were in danger.