Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible gcc bug with C++14 polymorphic lambdas?

I've found a strange behavior using polymorphic C++14 lambdas (lambdas with auto in their parameters):


Snippet 0:

#include <iostream>

template<typename T> void doLambda(T&& mFn)
{
  std::forward<T>(mFn)(int{0});
}

template<typename T> void test(T&& mV)
{
    doLambda([&mV](auto mE)
    {
        std::forward<decltype(mV)>(mV);
    });
}

int main() { test(int{0}); return 0; }

clang++ 3.5.1: the snippet compiles and runs successfully.

g++ 4.9.2: the snippet fails to compile:

example.cpp: In instantiation of 'test(T&&)::<lambda(auto:1)> [with auto:1 = int; T = int]':

5 : required from 'void doLambda(T&&) [with T = test(T&&) [with T = int]::]'
13 : required from 'void test(T&&) [with T = int]'
18 : required from here
12 : error: 'mV' was not declared in this scope

std::forward<decltype(mV)>(mV);
^

Compilation failed


Snippet 1:

The only difference from snippet 0 is that the auto inside the lambda was replaced to int.

#include <iostream>

template<typename T> void doLambda(T&& mFn)
{
  std::forward<T>(mFn)(int{0});
}

template<typename T> void test(T&& mV)
{
    doLambda([&mV](int mE)
    {
        std::forward<decltype(mV)>(mV);
    });
}

int main() { test(int{0}); return 0; }

clang++ 3.5.1: the snippet compiles and runs successfully.

g++ 4.9.2: the snippet compiles and runs successfully.


Snippet 3:

The lambda is now called in-place. auto is still used.

#include <iostream>

template<typename T> void test(T&& mV)
{
    [&mV](auto mE)
    {
        std::forward<decltype(mV)>(mV);
    }(int{0});
}

int main() { test(int{0}); return 0; }

clang++ 3.5.1: the snippet compiles and runs successfully.

g++ 4.9.2: the snippet compiles and runs successfully.


Why is g++ complaining about snippet 0? Is there anything wrong in my code? Is this a known bug or should I submit this?

like image 594
Vittorio Romeo Avatar asked Feb 01 '15 15:02

Vittorio Romeo


1 Answers

As stated in the comments, this behavior is indeed a gcc bug.

like image 162
Vittorio Romeo Avatar answered Oct 02 '22 15:10

Vittorio Romeo