This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/77585] New: g++ incorrectly decides that member function is called without object in generic lambda


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

            Bug ID: 77585
           Summary: g++ incorrectly decides that member function is called
                    without object in generic lambda
           Product: gcc
           Version: 6.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: neil.attn at ya dot ru
  Target Milestone: ---

I think I've found a bug regarding generic lambdas in g++. Clang accepts code.
$ cat gcc_maybe_bug.cc 
#include <algorithm>
#include <iterator>
#include <iostream>

template <typename T>
struct Base
{
        using value_type = T;

        void func(T v)
        {
                std::cout << v << a << '\n';
        }

        T a{5};
};

template <typename T>
struct Derived : T
{
        using typename T::value_type;

        // here's the bug, maybe?
        void do_something()
        {
                // Call member function of the base class. Everything's fine.
                T::func(arr[0]);

                // Everything is fine here also:
                auto lambda = [this](auto a) { T::func(a); };
                lambda(arr[1]);

                // Everything's fine here, too. Non-generic lambda.
                std::for_each(std::begin(arr), std::end(arr), [this](int a) {
T::func(a); });

                // Here's the error: g++ thinks, that T::func(a) is a call
without object, even
                // though "this" is explicitly captured. Same as above, but
generic lambda.
                std::for_each(std::begin(arr), std::end(arr), [this](auto a) {
T::func(a); });
        }

        value_type arr[16]{};
};

int main()
{
        Derived<Base<int>> o;
        o.do_something();
        return 0;
}
$ g++ -std=c++14 -Wall -Wextra -pedantic gcc_maybe_bug.cc 
gcc_maybe_bug.cc: In instantiation of
‘Derived<T>::do_something()::<lambda(auto:2)> [with auto:2 = int; T =
Base<int>]’:
/usr/include/c++/6.1.1/bits/stl_algo.h:3769:5:   required from ‘_Funct
std::for_each(_IIter, _IIter, _Funct) [with _IIter = int*; _Funct =
Derived<T>::do_something() [with T = Base<int>]::<lambda(auto:2)>]’
gcc_maybe_bug.cc:38:30:   required from ‘void Derived<T>::do_something() [with
T = Base<int>]’
gcc_maybe_bug.cc:47:24:   required from here
gcc_maybe_bug.cc:38:87: error: cannot call member function ‘void
Base<T>::func(T) [with T = int]’ without object
                 std::for_each(std::begin(arr), std::end(arr), [this](auto a) {
T::func(a); });

$ g++ --version
g++ (GCC) 6.1.1 20160621 (Red Hat 6.1.1-3)
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ clang++ -std=c++14 -Wall -Wextra -pedantic gcc_maybe_bug.cc

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]