http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57243

            Bug ID: 57243
           Summary: Using auto in range based for with templated container
                    in templated function requires extraneous template
                    qualifier
           Product: gcc
           Version: 4.8.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: w.shane.grant at gmail dot com

Using the auto keyword in a range based for loop under three conditions related
to templating requires an unnecessary use of the template qualifier for
compilation to happen successfully:

1. The container being iterated is templated
2. The range based for loop is inside of a template function
3. The iterated item has a template function

Here is a minimal example:

struct snarf
{
  template <class T>
  void get() {}
};

template <class T>
struct container
{
  snarf * begin() { return nullptr; }
  snarf * end() { return nullptr; }
};

template <class T>
void foo()
{
  container<int> arr;

  for( auto i : arr )
    i.get<int>();
}

int main()
{
  return 0;
}

The issue goes away if any of the following happen:
- foo is made a non template function
- arr is made a non template container
- auto is replaced with the actual type (snarf in this example)
- the function called on the range declaration (i) is not templated
- the template keyword is used to disambiguate the call to get
- the range based for loop is replaced with a standard for loop over the
iterators (auto works fine here)

Issue seems to happen with both versions of g++ I have installed (4.7.3 and g++
4.8.1 20130401) but not under clang (version 3.3 (trunk 178896)).

Reply via email to