接続数

COUNTER369477FROM 2012/5/16

MISRA-C diary(C言語日誌)

MISRA-C >> Article details

2014/06/13

C++N3242, 2011 (29) 5.1.2 Lambda expressions

Tweet ThisSend to Facebook | by kaizen
// 1 filename:cpp2011-5-1-2.cpp
// ver 0.1 June.12, 2014
//
// 2 original examples and/or notes:
// (c) ISO/IEC JTC1 SC22 WG21 N3242, April 12, 2011
// > 5 Expressions 5.1 Primary expressions 5.1.2 Lambda expressions
//
// 3 compile and output mechanism:
// (c) Dr. OGAWA Kiyoshi, kaizen at gifu-u.ac.jp,
//
// 4 compile errors and/or warnings:
// 4.1(c) Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
// Target: x86_64-apple-darwin13.2.0,  Thread model: posix
// Command/Options: c++ -std=c++11 -stdlib=libc++ -Wall cpp2011-5-1-2.cpp 
// (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.

// 4.2. g++-4.9 (GCC) 4.9.0 20131229 (experimental)
// Copyright (C) 2013 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.
// http://gcc.gnu.org/onlinedocs/gcc/Standards.html
// Command/Options: g++-4.9  -std=c++11  -Wall cpp2011-5-1-2.cpp 
// g++-4.9: error: unrecognized command line option '-stdlib=libc++'
// Configuration:brew install gcc49
//
// 4.3. Visual Studio Express 2013, 
// (c) Microsoft http://www.visualstudio.com/
// SPEC:
// Windows 7, .NET Framework
// (c) VMware, Inc.
// VMWare fusion 6
//
// 5. Hardware:  MacBook Pro, 
//(c) Intel http://ark.intel.com/products/37006/
//Core 2 Duo 2.53GHz, 8GB, 1067MHz DDR3
//
// 6. Special Thanks: Upper organizatios and 
// ITSCJ/IPSJ http://www.itscj.ipsj.or.jp/itscj_english/index.html
// Renesas Electronics Corporation.http://www.renesas.com/
// NPO SESSAME project, http://www.sessame.jp/workinggroup/WorkingGroup3/
// Toyo Corporation, http://www.toyo.co.jp/English/
// Japan Standard Association, http://bit.ly/1lzykg1
// NPO TOPPERS project, https://www.toppers.jp/asp-d-download.html
// Daido Universcity, http://www.daido-it.ac.jp/gakubugakka/computer/index.html
// WITZ Co.Ltd., http://www.witz-inc.co.jp/products/solution/solution.html
// SevenWise.co., http://www.7ws.co.jp/index.html
// TOYOTA Motor Corporation, http://toyota.jp/
// IT planning Inc., http://www.itpl.co.jp/en/index.html
// DENSO Corporation, http://www.globaldenso.com/en/
// Aisin Seiki co. Ltd., http://www.aisin.com/
// Spancion Inc., http://www.spansion.com/
// Yazaki Corporation, http://www.yazaki-group.com/global/
// Pananosic Corporation, http://www.panasonic.net/
// SWEST: Summer Workshop on Embedded System Technologies , http://swest.toppers.jp
// CEST: Consortium for Embedded System Technology, http://www.ertl.jp/CEST/
// JUSE: Union of Japanese Scientists and Engineers, http://www.juse.or.jp/e/
// OSC:Open Source Conference, http://www.ospn.jp/

#include <iostream>
//#include <cstdlib>
//#include <string.h>
//#include <cstring>
//#include <type_traits>
//#include <cassert>

using namespace std;
#include <algorithm>
#include <cmath>

void abssort(float *x, unsigned N) {
std::sort(x, x + N,
[](float a, float b) {
return std::abs(a) < std::abs(b);
});
}
//
auto x1 = [](int i){ return i; }; // OK: return type is int
// auto x2 = []{ return { 1, 2 }; }; // error: the return type is void (a
// braced-init-list is not an expression)
//
struct S1 {
int x, y;
int operator()(int);
void f() {
[=]()->int {
return operator()(this->x + y); // equivalent to S1::operator()(this->x + (*this).y)
// this has type S1*
};
}
};
//
struct S2 { void f(int i); };
void S2::f(int i) {
[&, i]{ }; // OK
// [&, &i]{ }; // error: i preceded by & when & is the default
// [=, this]{ }; // error: this when = is the default
// [i, i]{ }; // error: i repeated
}
//
void f1(int i) {
int const N = 20;
auto m1 = [=]{
int const M = 30;
auto m2 = [i]{
int x[N][M]; // OK: N and M are not odr-used
x[0][0] = i; // OK: i is explicitly captured by m2
// and implicitly captured by m1
};
};
struct s1 {
int f;
void work(int n) {
int m = n*n;
int j = 40;
auto m3 = [this,m] {
// auto m4 = [&,j] { // error: j not captured by m3
// int x = n; // error: n implicitly captured by m4
// but not captured by m3
int x += m; // OK: m implicitly captured by m4
// and explicitly captured by m3
// x += i; // error: i is outside of the reaching scope
x += f; // OK: this captured implicitly by m4
// and explicitly by m3
};
};
};
};
//}
//
void f2() {
int i = 1;
void g1(int = ([i]{ return i; })()); // ill-formed
void g2(int = ([i]{ return 0; })()); // ill-formed
void g3(int = ([=]{ return i; })()); // ill-formed
void g4(int = ([=]{ return 0; })()); // OK
void g5(int = ([]{ return sizeof i; })()); // OK
}
//
void f3() {
float x, &r = x;
[=] { // x and r are not captured (appearance in a decltype operand is not an odr-use)
decltype(x) y1; // y1 has type float
decltype((x)) y2 = y1; // y2 has type float const& because this lambda
// is not mutable and x is an lvalue
decltype(r) r1 = y1; // r1 has type float& (transformation not considered)
decltype((r)) r2 = y2; // r2 has type float const&
};
}
//
template<class... Args>
void f(Args... args) {
auto lm = [&, args...] { return g(args...); };
lm();
}
//
int main() {

cout << x1 <<std::endl;
int a = 1, b = 1, c = 1;
auto m1 = [a, &b, &c]() mutable {
auto m2 = [a, b, &c]() mutable {
std::cout << a << b << c;
a = 4; b = 4; c = 4;
};
a = 3; b = 3; c = 3;
m2();
};
a = 2; b = 2; c = 2;
m1();
std::cout << a << b << c;
cout << "5 Expressions 5.1 Primary expressions 5.1.2 Lambda expressions"<<std::endl;
return 0;
}
// error
//c++ -std=c++11 -stdlib=libc++ -Wall cpp2011-5-1-2.cpp 
cpp2011-5-1-2.cpp:85:1: warning: expression result unused [-Wunused-value]
[=]()->int {
^~~~~~~~~~~~
cpp2011-5-1-2.cpp:94:1: warning: expression result unused [-Wunused-value]
[&, i]{ }; // OK
^~~~~~~~~
cpp2011-5-1-2.cpp:104:6: warning: unused variable 'm2' [-Wunused-variable]
auto m2 = [i]{
     ^
cpp2011-5-1-2.cpp:119:7: error: invalid '+=' at end of declaration; did you mean '='?
int x += m; // OK: m implicitly captured by m4
      ^~
      =
cpp2011-5-1-2.cpp:132:16: error: lambda expression in default argument cannot capture any entity
void g1(int = ([i]{ return i; })()); // ill-formed
               ^
cpp2011-5-1-2.cpp:133:16: error: lambda expression in default argument cannot capture any entity
void g2(int = ([i]{ return 0; })()); // ill-formed
               ^
cpp2011-5-1-2.cpp:134:16: error: lambda expression in default argument cannot capture any entity
void g3(int = ([=]{ return i; })()); // ill-formed
               ^
cpp2011-5-1-2.cpp:146:15: warning: unused variable 'r2' [-Wunused-variable]
decltype((r)) r2 = y2; // r2 has type float const&
              ^
cpp2011-5-1-2.cpp:145:13: warning: unused variable 'r1' [-Wunused-variable]
decltype(r) r1 = y1; // r1 has type float& (transformation not considered)
            ^
cpp2011-5-1-2.cpp:141:1: warning: expression result unused [-Wunused-value]
[=] { // x and r are not captured (appearance in a decltype operand is not an odr-use)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 warnings and 4 errors generated.



08:48 | Impressed! | Voted(0) | Comment(0)