This is the mail archive of the gcc@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]

Problems with lambda implementation


Hello,

Recently I made some tests with lambda (mingw build) I found some
problems. Here they are:

1. Lambda and template type deduction.

Try this sample:

#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>

typedef std::function<double(double)> FnType;

template<typename T>
FnType average_damp(T fn)
{
    return [fn](double x) {return (x + fn(x)) / 2;};
}

int main(int argc, char* argv[])
{
    auto fn = average_damp([](double x){return x * 2;});

    std::cout << fn(10.0) << std::endl;

    return 0;
}

This sample successfully built with VS 2010, but under gcc there are
some compilation errors:
P:\projects\Tests\Sandbox\C++0x\gcc43>g++ -std=c++0x -Os -IP:\Projects\common\boost_1.36.0 lambda.cpp
In file included from s:/programming/mingw/lib/gcc/../../include/c++/4.5.0/functional:70:0,
                 from lambda.cpp:4:
s:/programming/mingw/lib/gcc/../../include/c++/4.5.0/tr1_impl/functional: In static member function 'static void std::_F
unction_base::_Base_manager<_Functor>::_M_clone(std::_Any_data&, const std::_Any_data&, std::false_type) [with _Functor
= average_damp(T) [with T = main(int, char**)::<lambda>(double), FnType = std::function<double(double)>]::<lambda>(doubl
e), std::false_type = std::integral_constant<bool, false>]':
s:/programming/mingw/lib/gcc/../../include/c++/4.5.0/tr1_impl/functional:1543:8:   instantiated from 'static bool std::_
Function_base::_Base_manager<_Functor>::_M_manager(std::_Any_data&, const std::_Any_data&, std::_Manager_operation) [wit
h _Functor = average_damp(T) [with T = main(int, char**)::<lambda>(double), FnType = std::function<double(double)>]::<la
mbda>(double)]'
s:/programming/mingw/lib/gcc/../../include/c++/4.5.0/tr1_impl/functional:2013:6:   instantiated from 'std::function<_Res
(_ArgTypes ...)>::function(_Functor, typename __gnu_cxx::__enable_if<(! std::is_integral::value), std::function<_Res(_Ar
gTypes ...)>::_Useless>::__type) [with _Functor = average_damp(T) [with T = main(int, char**)::<lambda>(double), FnType
= std::function<double(double)>]::<lambda>(double), _Res = double, _ArgTypes = double, typename __gnu_cxx::__enable_if<(
! std::is_integral::value), std::function<_Res(_ArgTypes ...)>::_Useless>::__type = std::function<double(double)>::_Usel
ess]'
lambda.cpp:11:51:   instantiated from 'FnType average_damp(T) [with T = main(int, char**)::<lambda>(double), FnType = st
d::function<double(double)>]'
lambda.cpp:16:55:   instantiated from here
s:/programming/mingw/lib/gcc/../../include/c++/4.5.0/tr1_impl/functional:1507:4: error: no matching function for call to
 'average_damp(T) [with T = main(int, char**)::<lambda>(double), FnType = std::function<double(double)>]::<lambda>(doubl
e)::__lambda0(average_damp(T) [with T = main(int, char**)::<lambda>(double), FnType = std::function<double(double)>]::<l
ambda>(double)&)'
lambda.cpp:11:25: note: candidates are: average_damp(T) [with T = main(int, char**)::<lambda>(double), FnType = std::fun
ction<double(double)>]::<lambda>(double)
s:/programming/mingw/lib/gcc/../../include/c++/4.5.0/tr1_impl/functional: In static member function 'static void std::_F
unction_base::_Base_manager<_Functor>::_M_init_functor(std::_Any_data&, const _Functor&, std::false_type) [with _Functor
 = average_damp(T) [with T = main(int, char**)::<lambda>(double), FnType = std::function<double(double)>]::<lambda>(doub
le), std::false_type = std::integral_constant<bool, false>]':
s:/programming/mingw/lib/gcc/../../include/c++/4.5.0/tr1_impl/functional:1555:4:   instantiated from 'static void std::_
Function_base::_Base_manager<_Functor>::_M_init_functor(std::_Any_data&, const _Functor&) [with _Functor = average_damp(
T) [with T = main(int, char**)::<lambda>(double), FnType = std::function<double(double)>]::<lambda>(double)]'
s:/programming/mingw/lib/gcc/../../include/c++/4.5.0/tr1_impl/functional:2014:6:   instantiated from 'std::function<_Res
(_ArgTypes ...)>::function(_Functor, typename __gnu_cxx::__enable_if<(! std::is_integral::value), std::function<_Res(_Ar
gTypes ...)>::_Useless>::__type) [with _Functor = average_damp(T) [with T = main(int, char**)::<lambda>(double), FnType
= std::function<double(double)>]::<lambda>(double), _Res = double, _ArgTypes = double, typename __gnu_cxx::__enable_if<(
! std::is_integral::value), std::function<_Res(_ArgTypes ...)>::_Useless>::__type = std::function<double(double)>::_Usel
ess]'
lambda.cpp:11:51:   instantiated from 'FnType average_damp(T) [with T = main(int, char**)::<lambda>(double), FnType = st
d::function<double(double)>]'
lambda.cpp:16:55:   instantiated from here
s:/programming/mingw/lib/gcc/../../include/c++/4.5.0/tr1_impl/functional:1584:4: error: no matching function for call to
 'average_damp(T) [with T = main(int, char**)::<lambda>(double), FnType = std::function<double(double)>]::<lambda>(doubl
e)::__lambda0(const average_damp(T) [with T = main(int, char**)::<lambda>(double), FnType = std::function<double(double)
>]::<lambda>(double)&)'
lambda.cpp:11:25: note: candidates are: average_damp(T) [with T = main(int, char**)::<lambda>(double), FnType = std::fun
ction<double(double)>]::<lambda>(double)

What's wrong?

2. Lambdas and result_of or something similar.

How I can determine return type of lambda in compile time? As far as I
could understood there is no way to do so. Is it right?

-- 
Best Regards,
 Sergey                          mailto:flex_ferrum@artberg.ru




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