https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70385

            Bug ID: 70385
           Summary: Lambda capture by reference of const reference fails
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nikb at bougalis dot net
  Target Milestone: ---

Consider the following minimal MCVE:

    const int& test()
    {
        int const static i = 0;
        return i;
    }

    int
    main()
    {
        auto square = [&j = test()]()
        {
        };

        return 0;
    }

The expectation here is that `pays` will be a `const int&`. Clang successfully
compiles this code (along with a warning about j being unused) but gcc fails to
compile with an interesting error:

error: binding 'const int' to reference of type 'int&' discards qualifiers
                 &j = test()
                     ~~~~~^~

This is clearly wrong. I believe that the correct behavior would be for `j` to
be a `const int&`.


We can simplify the example a bit more. The following code successfully
compiles and appears to do the "right" thing on both clang and gcc:

int
main()
{
    int const i = 0;

    auto test = [&i]()
    {
    };

    test();
    return 0;
}

The following code compiles and does the "right" thing in clang, but not in
gcc, where compilation fails:

int
main()
{
    int const i = 0;

    auto test = [&j = i]()
    {
    };

    test();
    return 0;
}

Reply via email to