接続数

COUNTER367233FROM 2012/5/16

MISRA-C diary(C言語日誌)

MISRA-C >> Article details

2014/06/16

C++N3242, 2011 (88) 8.5.4 List-initialization

Tweet ThisSend to Facebook | by kaizen
// 1 filename:cpp2011-8-5-4.cpp
// ver 0.1 June.13, 2014
//
// 2 original examples and/or notes:
// (c) ISO/IEC JTC1 SC22 WG21 N3242, April 12, 2011
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf
// > 8 Declarators 8.5 Initializers 8.5.4 List-initialization
//
// 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-8-5-4.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-8-5-4.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>
//#include <algorithm>
//#include <cmath>
#include <complex>
//#include <cstddef>
//#include <new>
#include <vector>

using namespace std;
template<typename T>

int a = {1};
std::complex<double> z{1,2};
new std::vector<std::string>{"once", "upon", "a", "time"}; // 4 string elements
f( {"Nicholas","Annemarie"} ); // pass list of two elements
return { "Norah" }; // return list of one element
int* e {}; // initialization to zero / null pointer
x = double{1}; // explicitly construct a double
std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
//
double ad[] = { 1, 2.0 }; // OK
//error: int ai[] = { 1, 2.0 }; // error: narrowing
struct S5 {
int m1;
double m2, m3;
};
S5 s21 = { 1, 2, 3.0 }; // OK
//error: S5 s22 { 1.0, 2, 3 }; // error: narrowing
S5 s23 { }; // OK: default to 0,0,0
//
struct S3 {
S3(std::initializer_list<double>); // #1
S3(std::initializer_list<int>); // #2
S3(); // #3
// ...
};
S3 s1 = { 1.0, 2.0, 3.0 }; // invoke #1
S3 s2 = { 1, 2, 3 }; // invoke #2
S3 s3 = { }; // invoke #3
//
struct Map {
Map(std::initializer_list<std::pair<std::string,int>>);
};
Map ship = {{"Sophie",14}, {"Surprise",28}};
//
struct S4 {
// no initializer-list constructors
S4(int, double, double); // #1
S4(); // #2
// ...
};
S4 s11 = { 1, 2, 3.0 }; // OK: invoke #1
//error: S4 s12 { 1.0, 2, 3 }; // error: narrowing
S4 s13 { }; // OK: invoke #2
//
struct S2 {
S2(std::initializer_list<double>); // #1
S2(const std::string&); // #2
// ...
};
const S2& r1 = { 1, 2, 3.0 }; // OK: invoke #1
const S2& r2 { "Spinach" }; // OK: invoke #2
//error: S2& r3 = { 1, 2, 3 }; // error: initializer is not an lvalue
//
int x1 {2}; // OK
//error: int x2 {2.0}; // error: narrowing
int** pp {}; // initialized to null pointer
//
struct A { int i; int j; };
A a1 { 1, 2 }; // aggregate initialization
//error: A a2 { 1.2 }; // error: narrowing
struct B {
B(std::initializer_list<int>);
};
B b1 { 1, 2 }; // creates initializer_list<int> and calls constructor
//error: B b2 { 1, 2.0 }; // error: narrowing
struct C {
C(int i, double j);
};
C c1 = { 1, 2.2 }; // calls constructor with arguments (1, 2.2)
//error: C c2 = { 1.1, 2 }; // error: narrowing
int j { 1 }; // initialize to 1
int k { }; // initialize to 0
//
struct X {
X(std::initializer_list<double> v);
};
X x3{ 1,2,3 };
//
double __a[3] = {double{1}, double{2}, double{3}};
X x4(std::initializer_list<double>(__a, __a+3));
//
typedef std::complex<double> cmplx;
std::vector<cmplx> v1 = { 1, 2, 3 };
void f3() {
std::vector<cmplx> v2{ 1, 2, 3 };
std::initializer_list<int> i3 = { 1, 2, 3 };
}
//
int x5 = 999; // x is not a constant expression
const int y = 999;
const int z5 = 99;
char c5 = x5; // OK, though it might narrow (in this case, it does narrow)
//error: char c2{x5}; // error: might narrow
//error: char c3{y}; // error: narrows (assuming char is 8 bits)
char c4{z5}; // OK: no narrowing needed
unsigned char uc1 = {5}; // OK: no narrowing needed
//error: unsigned char uc2 = {-1}; // error: narrows
//error: unsigned int ui1 = {-1}; // error: narrows
signed int si1 =
//error: { (unsigned int)-1 }; // error: narrows
//error: int ii = {2.0}; // error: narrows
//error: float f1 { x5 }; // error: might narrow
float f2 { 7 }; // OK: 7 can be exactly represented as a float
int f2(int);
int a6[] ={ 2, f2(2), f2(2.0) }; // OK: the double-to-int conversion is not at the top level

int main() {
cout << a6[0] << std::endl;
cout << "8 Declarators 8.5 Initializers 8.5.4 List-initialization" << std::endl;
return 0;
}
// 1 error
// 1.1 llvm: c++ -std=c++11 -stdlib=libc++  -Wall cpp2011-8-5-4.cpp 
cpp2011-8-5-4.cpp:75:5: warning: variable templates are a C++1y extension [-Wc++1y-extensions]
int a = {1};
    ^
cpp2011-8-5-4.cpp:77:1: error: expected unqualified-id
new std::vector<std::string>{"once", "upon", "a", "time"}; // 4 string elements
^
cpp2011-8-5-4.cpp:78:1: error: C++ requires a type specifier for all declarations
f( {"Nicholas","Annemarie"} ); // pass list of two elements
^
cpp2011-8-5-4.cpp:79:1: error: expected unqualified-id
return { "Norah" }; // return list of one element
^
cpp2011-8-5-4.cpp:81:1: error: C++ requires a type specifier for all declarations
x = double{1}; // explicitly construct a double
^
cpp2011-8-5-4.cpp:82:6: error: no template named 'map' in namespace 'std'; did you mean 'max'?
std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
~~~~~^~~
     max
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/algorithm:2600:1: note: 'max' declared here
max(const _Tp& __a, const _Tp& __b, _Compare __comp)
^
cpp2011-8-5-4.cpp:82:6: error: C++ requires a type specifier for all declarations
std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
~~~  ^
cpp2011-8-5-4.cpp:82:6: error: template specialization requires 'template<>'
std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
     ^  ~~~~~~~~~~~~~~~~~
template<> 
cpp2011-8-5-4.cpp:82:6: error: no variable template matches specialization
cpp2011-8-5-4.cpp:82:9: error: expected ';' after top level declarator
std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
        ^
        ;
cpp2011-8-5-4.cpp:154:6: error: no matching constructor for initialization of 'std::initializer_list<double>'
X x4(std::initializer_list<double>(__a, __a+3));
     ^                             ~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/initializer_list:66:5: note: candidate constructor not
      viable: no known conversion from 'double *' to 'size_t' (aka 'unsigned long') for 2nd argument; dereference the argument with *
    initializer_list(const _Ep* __b, size_t __s) _NOEXCEPT
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/initializer_list:81:5: note: candidate constructor not
      viable: requires 0 arguments, but 2 were provided
    initializer_list() _NOEXCEPT : __begin_(nullptr), __size_(0) {}
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/initializer_list:59:29: note: candidate constructor
      (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided
class _LIBCPP_TYPE_VIS_ONLY initializer_list
                            ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/initializer_list:59:29: note: candidate constructor
      (the implicit move constructor) not viable: requires 1 argument, but 2 were provided
cpp2011-8-5-4.cpp:160:28: warning: unused variable 'i3' [-Wunused-variable]
std::initializer_list<int> i3 = { 1, 2, 3 };
                           ^
cpp2011-8-5-4.cpp:177:7: error: expected '(' for function-style cast or type construction
float f2 { 7 }; // OK: 7 can be exactly represented as a float
~~~~~ ^
>2 warnings and 11 errors generated.
// 1.2 gcc: g++-4.9 -std=c++11 -Wall cpp2011-8-5-4.cpp 
cpp2011-8-5-4.cpp:75:5: error: template declaration of 'int a'
 int a = {1};
     ^
cpp2011-8-5-4.cpp:77:1: error: expected unqualified-id before 'new'
 new std::vector<std::string>{"once", "upon", "a", "time"}; // 4 string elements
 ^
cpp2011-8-5-4.cpp:78:2: error: expected constructor, destructor, or type conversion before '(' token
 f( {"Nicholas","Annemarie"} ); // pass list of two elements
  ^
cpp2011-8-5-4.cpp:78:29: error: expected unqualified-id before ')' token
 f( {"Nicholas","Annemarie"} ); // pass list of two elements
                             ^
cpp2011-8-5-4.cpp:79:1: error: expected unqualified-id before 'return'
 return { "Norah" }; // return list of one element
 ^
cpp2011-8-5-4.cpp:81:1: error: 'x' does not name a type
 x = double{1}; // explicitly construct a double
 ^
cpp2011-8-5-4.cpp:82:6: error: 'map' in namespace 'std' does not name a template type
 std::map<std::string,int> anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} };
      ^
cpp2011-8-5-4.cpp:154:44: error: invalid conversion from 'double*' to 'std::initializer_list<double>::size_type {aka long unsigned int}' [-fpermissive]
 X x4(std::initializer_list<double>(__a, __a+3));
                                            ^
In file included from /usr/local/Cellar/gcc49/4.9-20131229/lib/gcc/x86_64-apple-darwin13.0.0/4.9.0/include/c++/bits/basic_string.h:42:0,
                 from /usr/local/Cellar/gcc49/4.9-20131229/lib/gcc/x86_64-apple-darwin13.0.0/4.9.0/include/c++/string:52,
                 from /usr/local/Cellar/gcc49/4.9-20131229/lib/gcc/x86_64-apple-darwin13.0.0/4.9.0/include/c++/bits/locale_classes.h:40,
                 from /usr/local/Cellar/gcc49/4.9-20131229/lib/gcc/x86_64-apple-darwin13.0.0/4.9.0/include/c++/bits/ios_base.h:41,
                 from /usr/local/Cellar/gcc49/4.9-20131229/lib/gcc/x86_64-apple-darwin13.0.0/4.9.0/include/c++/ios:42,
                 from /usr/local/Cellar/gcc49/4.9-20131229/lib/gcc/x86_64-apple-darwin13.0.0/4.9.0/include/c++/ostream:38,
                 from /usr/local/Cellar/gcc49/4.9-20131229/lib/gcc/x86_64-apple-darwin13.0.0/4.9.0/include/c++/iostream:39,
                 from cpp2011-8-5-4.cpp:59:
/usr/local/Cellar/gcc49/4.9-20131229/lib/gcc/x86_64-apple-darwin13.0.0/4.9.0/include/c++/initializer_list:62:17: note: initializing argument 2 of 'constexpr std::initializer_list<_E>::initializer_list(std::initializer_list<_E>::const_iterator, std::initializer_list<_E>::size_type) [with _E = double; std::initializer_list<_E>::const_iterator = const double*; std::initializer_list<_E>::size_type = long unsigned int]'
       constexpr initializer_list(const_iterator __a, size_type __l)
                 ^
/usr/local/Cellar/gcc49/4.9-20131229/lib/gcc/x86_64-apple-darwin13.0.0/4.9.0/include/c++/initializer_list:62:17: error: 'constexpr std::initializer_list<_E>::initializer_list(std::initializer_list<_E>::const_iterator, std::initializer_list<_E>::size_type) [with _E = double; std::initializer_list<_E>::const_iterator = const double*; std::initializer_list<_E>::size_type = long unsigned int]' is private
cpp2011-8-5-4.cpp:154:46: error: within this context
 X x4(std::initializer_list<double>(__a, __a+3));
                                              ^
cpp2011-8-5-4.cpp: In function 'void f3()':
cpp2011-8-5-4.cpp:160:28: warning: unused variable 'i3' [-Wunused-variable]
 std::initializer_list<int> i3 = { 1, 2, 3 };
                            ^
cpp2011-8-5-4.cpp: At global scope:
cpp2011-8-5-4.cpp:177:1: error: expected primary-expression before 'float'
 float f2 { 7 }; // OK: 7 can be exactly represented as a float
 ^
ogawa-sei-no-MacBook-Pro-3:misrac kaizen$ 


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