Skip to content

CWG 1442: Argument-dependent lookup in the range-based for #592

In review

Description

@nv-cmeerw

Issue Summary

The front end incorrectly accepts range-based for statements whose begin and end functions are found only by ordinary unqualified lookup or only in a non-associated namespace std. CWG 1442 requires these names to be found by argument-dependent lookup alone, without treating std as an associated namespace unless it is associated with the range type.

Reproducing Source Code

namespace N {
  struct A {};
  struct B {};
}

// Visible to ordinary unqualified lookup, but not to ADL.
int *begin(N::A &);
int *end(N::A &);

namespace std {
  // std is not associated with N::B.
  int *begin(N::B &);
  int *end(N::B &);
}

void f(N::A &a, N::B &b) {
  for (int x : a) {}
  for (int x : b) {}
}

Command-line Options

--c++11

Type of Issue

[Front End] This code SHOULD NOT compile with these options.

Additional Details

CWG 1442 clarifies that begin(__range) and end(__range) are looked up in the associated namespaces. Ordinary unqualified lookup is not performed, and namespace std is not automatically an associated namespace.

Both loops above should therefore be rejected because no suitable begin or end function can be found by argument-dependent lookup. The first loop incorrectly uses the global functions, while the second incorrectly uses the functions declared in std.

Reproducing (Standard) Configuration(s)

linux-gcc-debug

Reproducing (Non-standard) Configuration

No response

I acknowledge that:

  • I have checked open issues and existing discussions before filing this report.
  • The reproducing source code that I have provided does not depend on headers that I have not included with my submission.
  • I have reduced the reproducing source code to the best of my ability.
  • I have selected at least one standard configuration or uploaded a non-standard configuration dump.
  • I understand code shared via this form is NOT confidential.
  • I have permission to share the reproducing source code and grant permission for the project to use the source code (or derivatives of the source code) to create tests.
  • I agree that any tests created using the reproducing source code (or deriatives of it) will be licensed under project license unless I included a LICENSE.txt file in my .zip or .tar.gz upload. If I included a LICENSE.txt file, any tests created using the reproducing source code will be governed by the terms of the license I provided instead.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions