r/cpp • • Apr 24 '26

C++26: Structured Bindings can introduce a Pack

https://www.sandordargo.com/blog/2026/04/22/cpp26-structured-bindings-packs
101 Upvotes

40 comments sorted by

View all comments

Show parent comment

1

u/AvidCoco Apr 25 '26

You could argue the same for std::get that using the index-based version breaks type safety and you should only ever use the type-based version.

In some cases you want something by index.

1

u/Ateist Apr 27 '26 edited Apr 27 '26

You could argue the same for std::get that using the index-based version breaks type safety and you should only ever use the type-based version.

can you give an example that is not detected by a compiler?

because those cases that I looked up it absolutely does

i.e. index-based version:

void myfunc(int x);

std::tuple<int, std::string> myData = {10, "Hello"};
auto value = std::get<0>(myData); 
myfunc(value);  //no error

std::tuple<Errortype, std::string> myData1 = {Errortype::some_value, "Hello"};
auto value2 = std::get<0>(myData1); 
myfunc(value2);  //Compiler error - wrong type supplied: wanted int, got Errortype

So it is just as safe as type-based version.