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 #include #include #include typedef std::function FnType; template 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**)::(double), FnType = std::function]::(doubl e), std::false_type = std::integral_constant]': 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**)::(double), FnType = std::function]::(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**)::(double), FnType = std::function]::(double), _Res = double, _ArgTypes = double, typename __gnu_cxx::__enable_if<( ! std::is_integral::value), std::function<_Res(_ArgTypes ...)>::_Useless>::__type = std::function::_Usel ess]' lambda.cpp:11:51: instantiated from 'FnType average_damp(T) [with T = main(int, char**)::(double), FnType = st d::function]' 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**)::(double), FnType = std::function]::(doubl e)::__lambda0(average_damp(T) [with T = main(int, char**)::(double), FnType = std::function]::(double)&)' lambda.cpp:11:25: note: candidates are: average_damp(T) [with T = main(int, char**)::(double), FnType = std::fun ction]::(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**)::(double), FnType = std::function]::(doub le), std::false_type = std::integral_constant]': 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**)::(double), FnType = std::function]::(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**)::(double), FnType = std::function]::(double), _Res = double, _ArgTypes = double, typename __gnu_cxx::__enable_if<( ! std::is_integral::value), std::function<_Res(_ArgTypes ...)>::_Useless>::__type = std::function::_Usel ess]' lambda.cpp:11:51: instantiated from 'FnType average_damp(T) [with T = main(int, char**)::(double), FnType = st d::function]' 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**)::(double), FnType = std::function]::(doubl e)::__lambda0(const average_damp(T) [with T = main(int, char**)::(double), FnType = std::function]::(double)&)' lambda.cpp:11:25: note: candidates are: average_damp(T) [with T = main(int, char**)::(double), FnType = std::fun ction]::(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_fer...@artberg.ru
Is this code legal?
Hello all. I try to compile the following code sample: (with MinGW gcc version 4.5.0 20090819 (experimental) (GCC)) #include #include #include template struct Message { std::array m_Message; Message() : m_Message{Chars...} // {*1} { std::array msg = {Chars ... }; // {*2} } }; int main(int argc, char* argv[]) { setlocale(LC_ALL, ".OCP"); Message msg; std::for_each(msg.m_Message.begin(), msg.m_Message.end(), [](wchar_t c){std::wcout << c;}); return 0; } Can anybody explain why line marked with '{*1}' produce this error message: hello_programmer.cpp: In constructor 'Message::Message() [with wchar_t ...Chars = 1057u, 32u, 1076u, 1085u, 1105u , 1084u, 32u, 1087u, 1088u, 1086u, 1075u, 1088u, 1072u, 1084u, 1084u, 1080u, 1089u, 1090u, 1072u, 44u, 32u, 1082u, 1086u , 1083u, 1083u, 1077u, 1075u, 1080u, 33u, 10u]': hello_programmer.cpp:48:15: instantiated from here hello_programmer.cpp:34:35: error: could not convert '{1057u, 32u, 1076u, 1085u, 1105u, 1084u, 32u, 1087u, 1088u, 1086u, 1075u, 1088u, 1072u, 1084u, 1084u, 1080u, 1089u, 1090u, 1072u, 44u, 32u, 1082u, 1086u, 1083u, 1083u, 1077u, 1075u, 1080 u, 33u, 10u}' to 'std::array' Line marked with '{*2}' is compiled successfully. Or there is no standard-conforming way to initialize std::array within constructor initializer list? - Best regards, Sergey.
Re[2]: Is this code legal?
Hello, Paolo. Tuesday, October 6, 2009 at 2:05:10 PM you wrote: PB> On 10/05/2009 09:29 PM, Sergey Sadovnikov wrote: >> Can anybody explain why line marked with '{*1}' produce this error >> message: PB> I think it's because there is no constructor for array that takes an PB> initializer_list. I get this message if I change your {*2} line to: No. I think it's because my version of gcc compiler was without 'Standard layout types' C++0x extension. There were no errors after I updated my trunk, recompile gcc and then recompile the sample. PB> In general, this kind of question is best asked on a C++ forum. Probably you're right. -- Best Regards, Sergey mailto:flex_fer...@artberg.ru
Linkage problem with rescent gcc 4.5 in MinGW build
Hello. There is linkage problem with recent gcc 4.5 in MinGW configuration build. Executables which are produced by gcc with default options set treated as 'not a valid win32 application' by the system (Win2008 x64 server). After stripping with the 'strip' utility executables are successfully ran. I suppose that problems in 'debug-xxx' sections generated by the new gcc/linker. I made the section dumps of the 'good' and 'bad' executable produced by the gcc 4.4 and gcc 4.5 with the same options set. Here they are: Section produced by gcc 4.4: 0 .text 9a2c 00401000 00401000 0400 2**4 CONTENTS, ALLOC, LOAD, READONLY, CODE, DATA 1 .data 0040 0040b000 0040b000 a000 2**2 CONTENTS, ALLOC, LOAD, DATA 2 .rdata17c0 0040c000 0040c000 a200 2**5 CONTENTS, ALLOC, LOAD, READONLY, DATA 3 .bss 0b20 0040e000 0040e000 2**5 ALLOC 4 .idata051c 0040f000 0040f000 ba00 2**2 CONTENTS, ALLOC, LOAD, DATA 5 .debug_aranges 0060 0041 0041 c000 2**3 CONTENTS, READONLY, DEBUGGING 6 .debug_pubnames 0076 00411000 00411000 c200 2**0 CONTENTS, READONLY, DEBUGGING 7 .debug_info 0d4d 00412000 00412000 c400 2**0 CONTENTS, READONLY, DEBUGGING 8 .debug_abbrev 037c 00413000 00413000 d200 2**0 CONTENTS, READONLY, DEBUGGING 9 .debug_line 0316 00414000 00414000 d600 2**0 CONTENTS, READONLY, DEBUGGING 10 .debug_frame 0070 00415000 00415000 da00 2**2 CONTENTS, READONLY, DEBUGGING 11 .debug_loc0539 00416000 00416000 dc00 2**0 CONTENTS, READONLY, DEBUGGING 12 .debug_ranges 00f8 00417000 00417000 e200 2**0 CONTENTS, READONLY, DEBUGGING Sections produced by gcc 4.5: Sections: Idx Name Size VMA LMA File off Algn 0 .debug_pubtypes 017c 03d0 2**0 CONTENTS, READONLY, DEBUGGING 1 .text 9a2c 00401000 00401000 0600 2**4 CONTENTS, ALLOC, LOAD, READONLY, CODE, DATA 2 .data 0040 0040b000 0040b000 a200 2**2 CONTENTS, ALLOC, LOAD, DATA 3 .rdata17c0 0040c000 0040c000 a400 2**5 CONTENTS, ALLOC, LOAD, READONLY, DATA 4 .bss 0b20 0040e000 0040e000 2**5 ALLOC 5 .idata051c 0040f000 0040f000 bc00 2**2 CONTENTS, ALLOC, LOAD, DATA 6 .debug_aranges 00e0 0041 0041 c200 2**3 CONTENTS, READONLY, DEBUGGING 7 .debug_pubnames 0317 00411000 00411000 c400 2**0 CONTENTS, READONLY, DEBUGGING 8 .debug_info 2b04 00412000 00412000 c800 2**0 CONTENTS, READONLY, DEBUGGING 9 .debug_abbrev 08e2 00415000 00415000 f400 2**0 CONTENTS, READONLY, DEBUGGING 10 .debug_line 0882 00416000 00416000 fe00 2**0 CONTENTS, READONLY, DEBUGGING 11 .debug_frame 05f0 00417000 00417000 00010800 2**2 CONTENTS, READONLY, DEBUGGING 12 .debug_str007a 00418000 00418000 00010e00 2**0 CONTENTS, READONLY, DEBUGGING 13 .debug_loc1e11 00419000 00419000 00011000 2**0 CONTENTS, READONLY, DEBUGGING 14 .debug_ranges 0198 0041b000 0041b000 00013000 2**0 CONTENTS, READONLY, DEBUGGING As you can see there is two new debug sections ('.debug_pubtypes' and '.debug_str') and .debug_pubtypes is the first section in the executable. gcc build configured with following options: --prefix=/mingw-4.5 --host=mingw32 --target=mingw32 --program-prefix= --with-as=/mingw/bin/as.exe --with-ld=/mingw/bin/ld.exe --with-gcc --with-gnu-ld --with-gnu-as --enable-threads --disable-nls --enable-languages=c,c++ --disable-win32-registry --disable-shared --without-x --enable-interpreter --enable-hash-synchronization --enable-libstdcxx-debug --with-gmp-include=/projects/common/GMP/4.3.1 --with-gmp-lib=/projects/common/GMP/4.3.1/.libs --with-mpfr-include=/projects/common/MPFR/2.4.1 --with-mpfr-lib=/projects/common/MPFR/2.4.1/.libs --enable-bootstrap Because of I'm not a specialist in compilers and linkers so I can't fix this problem by myself. -- Best Regards, Sergey mailto:flex_fer...@artberg.ru