0
for (auto [i, j] : vector<tuple<int, int>>{{1, 7}, {3, 2}})
  cout << i << j; 

Is there a way to make this range-based for loop more concise by omitting type specification of the container? I don't care about its actual type, as long as it contains (mathematical not C++) pairs or tuples of integers. The form below would be the best, but it doesn't compile:

for (auto [i, j] : {{1, 7}, {3, 2}})
  cout << i << j; 

EDIT:

This question is not the same as Range-based for over pair list, because I use structured binding. I don't want to use first or get<0> for access. I'm using pair in mathematical and common sense, not std::pair.

Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72
  • 2
    Sadly (for my excitement) looks like an exact duplicate, unless you're trying to generalize somehow. – miradulo Aug 10 '18 at 01:13
  • Is it ok for you to use `std::pair` instead of `std::tuple`? – Coral Kashri Aug 10 '18 at 01:43
  • @KorelK No, because I would not be able to use meaningful names: `i` becomes a generic `first` or `get<0>`. – Paul Jurczak Aug 10 '18 at 01:52
  • @miradulo I added an explanation why it is a different question. Let me know, if it is not satisfactory. See also my response to *KorelK* comment. – Paul Jurczak Aug 10 '18 at 01:55
  • So I don't think that this question is a duplicate. The "duplicate" question is about `pair` issu, and not about `tuple` one. I think that @PaulJurczak is right. – Coral Kashri Aug 10 '18 at 01:56
  • 1
    @PaulJurczak You can use structured bindings with a `std::pair`.. sorry I'm not quite following. Why would `for (auto [i, j] : {std::pair{1, 7}, {3, 2}})` not be satisfactory? – miradulo Aug 10 '18 at 01:56
  • @miradulo It would, but it doesn't compile with current version of Microsoft C++. I will try a different compiler. – Paul Jurczak Aug 10 '18 at 02:01
  • @PaulJurczak Oh hmm.. `std::make_pair` neither? – miradulo Aug 10 '18 at 02:02
  • Compiles on Clang 6.0.1 and GCC 7.3.0, at the very least. – miradulo Aug 10 '18 at 02:09
  • `for (auto& val : vector>{tuple(1, 7), tuple(3, 2)})` this work for me. – Coral Kashri Aug 10 '18 at 02:10
  • @miradulo Yes, it works with gcc 8.2.0. If you post it, I will accept it as an answer. – Paul Jurczak Aug 10 '18 at 02:11
  • @PaulJurczak I still think it is a duplicate as it is the exact same idea. Glad you got it working! – miradulo Aug 10 '18 at 02:12
  • @KorelK 1. It doesn't use structured binding. 2. It is more verbose than original. – Paul Jurczak Aug 10 '18 at 02:13
  • @miradulo `auto pair` IS NOT the same as `auto [nameFullOfMeaning, nameEvenFullerOfMeaning]`. You lose semantics using 'std::pair` representation. – Paul Jurczak Aug 10 '18 at 02:16
  • There's no need to yell at me, thanks. The heart of your question appears to be how to make `vector>{...}` more concise - my answer that I originally posted and deleted is virtually identical to StoryTeller's except I include your structured binding. Structured bindings do not change the question nor the answer (besides leaving them in), so it looks like a duplicate. – miradulo Aug 10 '18 at 02:19
  • @miradulo I'm sorry, if it was too loud. I tried to explain several times, why it makes semantic difference. It also makes a practical difference, due to structured binding being a new C++ feature. I was not aware that it can be freely exchanged with `std::pair` and so is my Visual C++ compiler. If this is not enough difference for you, keep your duplicate mark. Someone in the same situation in the future will have to look a bit harder, but the precious bytes on SO server will be freed. – Paul Jurczak Aug 10 '18 at 02:41

0 Answers0