[local] "true" lambdas without C++11 but only on GCC

Hello all, A curiosity: On GCC compilers (but _only_ on GCC compilers) it is possible to combine Boost.Local and the non-standard GCC "expression statements" feature to implement lambda functions. In other words, it is possible to define a Boost.Local closure within a GCC expression statement: std::vector<int> v(3); v[0] = 1; v[1] = 2; v[2] = 3; int factor = 10; std::for_each(v.begin(), v.end(), LAMBDA(int x, const bind& factor) { std::cout << x * factor << std::endl; } LAMBDA_END ); This is just a curiosity because it is NOT standard C++03 and it will ONLY work with GCC compilers. #include <boost/local/function.hpp> #include <vector> #include <algorithm> #include <iostream> // WARNING: This macros only work on GCC!!! #define LAMBDA_WITH_RESULT(result_type, ...) \ ({ /* open GCC statement expression */ \ result_type BOOST_LOCAL_FUNCTION_PARAMS(__VA_ARGS__) #define LAMBDA(...) \ LAMBDA_WITH_RESULT(void, __VA_ARGS__) #define LAMBDA_END \ BOOST_LOCAL_FUNCTION_NAME(BOOST_PP_CAT(lambda, __LINE__)) \ BOOST_PP_CAT(lambda, __LINE__); \ }) /* close GCC statement expression */ int main() { std::vector<int> v(3); v[0] = 1; v[1] = 2; v[2] = 3; int factor = 10; std::for_each(v.begin(), v.end(), LAMBDA(int x, const bind& factor) { std::cout << x * factor << std::endl; } LAMBDA_END ); return 0; } I might mention this code example as a curiosity in Boost.Local docs, currently it's not mentioned there. Thanks. --Lorenzo

on Thu Nov 17 2011, Lorenzo Caminiti <lorcaminiti-AT-gmail.com> wrote:
Hello all,
A curiosity: On GCC compilers (but _only_ on GCC compilers) it is possible to combine Boost.Local and the non-standard GCC "expression statements" feature to implement lambda functions. In other words, it is possible to define a Boost.Local closure within a GCC expression statement:
std::vector<int> v(3); v[0] = 1; v[1] = 2; v[2] = 3; int factor = 10;
std::for_each(v.begin(), v.end(), LAMBDA(int x, const bind& factor) { std::cout << x * factor << std::endl; } LAMBDA_END );
This is just a curiosity because it is NOT standard C++03 and it will ONLY work with GCC compilers.
I think other compilers such as EDG and Clang may support this extension for GCC-compatibility. -- Dave Abrahams BoostPro Computing http://www.boostpro.com

On Mon, Nov 28, 2011 at 7:02 PM, Dave Abrahams <dave@boostpro.com> wrote:
on Thu Nov 17 2011, Lorenzo Caminiti <lorcaminiti-AT-gmail.com> wrote:
A curiosity: On GCC compilers (but _only_ on GCC compilers) it is possible to combine Boost.Local and the non-standard GCC "expression statements" feature to implement lambda functions. In other words, it is possible to define a Boost.Local closure within a GCC expression statement:
std::vector<int> v(3); v[0] = 1; v[1] = 2; v[2] = 3; int factor = 10;
std::for_each(v.begin(), v.end(), LAMBDA(int x, const bind& factor) { std::cout << x * factor << std::endl; } LAMBDA_END );
This is just a curiosity because it is NOT standard C++03 and it will ONLY work with GCC compilers.
I think other compilers such as EDG and Clang may support this extension for GCC-compatibility.
Yes, I found on-line that these compilers support expression statements: http://stackoverflow.com/questions/6440021/compiler-support-of-gnu-statement... GCC >=3 Intel C++ Compiler >=? Clang/LLVM >= ? Sun Studio >= 12 (New Language Extensions) IBM XL for z/OS (marked as IBM extension) Open64 (as it use osprey-gcc frontend) PathScale® EKOPath Any more? EDG? Comeau? The LAMBDA macros could be implemented to expand using C++11 lambdas on compilers that have them (MSVC>=10, later GCC, etc), to expand using expression statements + Boost.Local on the above compilers, and to generate a compiler error otherwise. That way we'd have "true" lambdas for MSVC plus the compilers above... Would this be useful to Boost? Thanks a lot. --Lorenzo

on Tue Nov 29 2011, Lorenzo Caminiti <lorcaminiti-AT-gmail.com> wrote:
Yes, I found on-line that these compilers support expression statements: http://stackoverflow.com/questions/6440021/compiler-support-of-gnu-statement...
GCC >=3 Intel C++ Compiler >=? Clang/LLVM >= ? Sun Studio >= 12 (New Language Extensions) IBM XL for z/OS (marked as IBM extension) Open64 (as it use osprey-gcc frontend) PathScale® EKOPath Any more? EDG? Comeau?
Intel and Comeau are EDG front-ends, so they are all in the same bucket. -- Dave Abrahams BoostPro Computing http://www.boostpro.com
participants (2)
-
Dave Abrahams
-
Lorenzo Caminiti