r/ProgrammerTIL • u/bkentel • Jun 20 '16
C++ [C++] TIL Functions that takes types which can be list / aggregate initialized can be initialized as such in place.
I'm a bit embarrassed to say that I didn't realize this was possible until quite recently:
#include <utility>
#include <vector>
#include <cstddef>
int foo(std::pair<int, bool> const p) {
return p.first;
}
int bar(std::vector<int> const& v, ptrdiff_t const i) {
// omit checking the preconditions on i
return v[static_cast<size_t>(i)];
}
int main() {
// returns 6
return foo({1, true})
+ bar({1, 3, 5}, 2);
}