Foreach loops written with a structured-binding declaration error out if any of the captured types contains a template member-function which is used inside the loop. This only happens if such a loop is placed inside a templated lambda/function. A minimal example of the above is: struct Foo { template<typename T> T get() const { return static_cast<T>(0); } }; template<typename T> void error_func(T elem) { std::array<std::pair<int, Foo>, 1> arr; for(const auto& [_, val] : arr) { val.get<int>(); // Error here } } The error disappears if T is replaced with any concrete type. It also disappears if instead of structured bindings, a tuple is used with auto type specifier. The above code also works if one writes: const auto& [_, val] = arr[0]; val.get<int>(); Compilation flags: -std=c++17 Reported error: test.cpp:19:17: error: expected primary-expression before 'int' val.get<int>(); ^~~ test.cpp:19:17: error: expected ';' before 'int' val.get<int>(); ^~~ ;
My bet is gcc mistaken it for being a dependent type which meaning you can use the template keyword to workaround the issue. Also gcc 8.x series is over 3 years old so this might be fixed already in a newer version of gcc.
Yeah .template works as well. This is in the latest release too. I wasn't sure whether I should write the oldest version that this is in (since other 8.x releases would need a patch as well), or the latest. Let me change that.
Reduced testcase (without any includes): struct Foo { template<class T> T get() const { return 0; } }; struct f { int t; Foo t1; }; template<typename T> void error_func(T elem) { f arr[1]; for(const auto& [_, val] : arr) { val.get<int>(); // Error here } }
Dup of bug 84469. *** This bug has been marked as a duplicate of bug 84469 ***