// 1 filename:cpp2011-8-3-6.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.3 Meaning of declarators 8.3.6 Default arguments // // 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-3-6.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-3-6.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 //#include //#include //#include //#include //#include //#include //#include //#include //#include //#include using namespace std; void point(int = 3, int = 4); // void g(int = 0, ...); // OK, ellipsis is not a parameter so it can follow // a parameter with a default argument void f(int, int); void f(int, int = 7); void h() { f(3); // OK, calls f(3, 7) //error: void f(int = 1, int); // error: does not use default // from surrounding scope } void m() { void f(int, int); // has no defaults //error: f(4); // error: wrong number of arguments void f(int, int = 5); // OK f(4); // OK, calls f(4, 5); //error: void f(int, int = 5); // error: cannot redefine, even to // same value } void n() { f(6); // OK, calls f(6, 7) } // int a = 1; int f(int); int g(int x = f(a)); // default argument: f(::a) void h2() { a = 2; { int a = 3; g(); // g(f(::a)) } } // class C { void f(int i = 3); void g(int i, int j = 99); }; //error: void C::f(int i = 3) { // error: default argument already //} // specified in class scope void C::g(int i = 88, int j) { // in this translation unit, } // C::g can be called with no argument // void f() { int i; //error: extern void g(int x = i); //error // ... } // class A { //error: void f(A* p = this) { } // error }; // int a2; //error: int f(int a, int b = a); // error: parameter a // used as default argument typedef int I; //error: int g(float I, int b = I(2)); // error: parameter I found //error: int h(int a, int b = sizeof(a)); // error, parameter a used // in default argument // int b; class X { int a; //error: int mem1(int i = a); // error: non-static member a // used as default argument int mem2(int i = b); // OK; use X::b static int b; }; // int f(int = 0); void h3() { int j = f(1); int k = f(); // OK, means f(0) } int (*p1)(int) = &f; //error: int (*p2)() = &f; // error: type mismatch // struct A2 { virtual void f(int a = 7); }; struct B : public A2 { void f(int a); }; void m2() { B* pb = new B; A2* pa = pb; pa->f(); // OK, calls pa->B::f(7) //error: pb->f(); // error: wrong number of arguments for B::f() } int main() { // point(1,2); point(1); point(); cout << a2 << std::endl; cout << "8 Declarators 8.3 Meaning of declarators 8.3.6 Default arguments" << std::endl; return 0; } // 1 error: // 1.1 llvm:c++ -std=c++11 -stdlib=libc++ -Wall cpp2011-8-3-6.cpp cpp2011-8-3-6.cpp:99:15: error: call to 'f' is ambiguous int g(int x = f(a)); // default argument: f(::a) ^ cpp2011-8-3-6.cpp:79:6: note: candidate function void f(int, int = 7); ^ cpp2011-8-3-6.cpp:98:5: note: candidate function int f(int); ^ cpp2011-8-3-6.cpp:103:5: warning: unused variable 'a' [-Wunused-variable] int a = 3; ^ cpp2011-8-3-6.cpp:118:5: warning: unused variable 'i' [-Wunused-variable] int i; ^ cpp2011-8-3-6.cpp:146:9: error: call to 'f' is ambiguous int j = f(1); ^ cpp2011-8-3-6.cpp:79:6: note: candidate function void f(int, int = 7); ^ cpp2011-8-3-6.cpp:144:6: note: candidate function int f(int = 0); ^ cpp2011-8-3-6.cpp:147:9: error: call to 'f' is ambiguous int k = f(); // OK, means f(0) ^ cpp2011-8-3-6.cpp:117:6: note: candidate function void f() { ^ cpp2011-8-3-6.cpp:144:6: note: candidate function int f(int = 0); ^ > 2 warnings and 3 errors generated. // 1.2 gcc: g++-4.9 -std=c++11 -Wall cpp2011-8-3-6.cpp cpp2011-8-3-6.cpp:99:18: error: call of overloaded 'f(int&)' is ambiguous int g(int x = f(a)); // default argument: f(::a) ^ cpp2011-8-3-6.cpp:99:18: note: candidates are: cpp2011-8-3-6.cpp:79:6: note: void f(int, int) void f(int, int = 7); ^ cpp2011-8-3-6.cpp:98:5: note: int f(int) int f(int); ^ cpp2011-8-3-6.cpp: In function 'void h2()': cpp2011-8-3-6.cpp:104:3: error: call of overloaded 'g()' is ambiguous g(); // g(f(::a)) ^ cpp2011-8-3-6.cpp:104:3: note: candidates are: cpp2011-8-3-6.cpp:76:6: note: void g(int, ...) void g(int = 0, ...); // OK, ellipsis is not a parameter so it can follow ^ cpp2011-8-3-6.cpp:99:5: note: int g(int) int g(int x = f(a)); // default argument: f(::a) ^ cpp2011-8-3-6.cpp:103:5: warning: unused variable 'a' [-Wunused-variable] int a = 3; ^ cpp2011-8-3-6.cpp: In function 'void f()': cpp2011-8-3-6.cpp:118:5: warning: unused variable 'i' [-Wunused-variable] int i; ^ cpp2011-8-3-6.cpp: In function 'void h3()': cpp2011-8-3-6.cpp:146:12: error: call of overloaded 'f(int)' is ambiguous int j = f(1); ^ cpp2011-8-3-6.cpp:146:12: note: candidates are: cpp2011-8-3-6.cpp:79:6: note: void f(int, int) void f(int, int = 7); ^ cpp2011-8-3-6.cpp:144:6: note: int f(int) int f(int = 0); ^ cpp2011-8-3-6.cpp:147:11: error: call of overloaded 'f()' is ambiguous int k = f(); // OK, means f(0) ^ cpp2011-8-3-6.cpp:147:11: note: candidates are: cpp2011-8-3-6.cpp:144:6: note: int f(int) int f(int = 0); ^ cpp2011-8-3-6.cpp:117:6: note: void f() void f() { ^ cpp2011-8-3-6.cpp:146:5: warning: unused variable 'j' [-Wunused-variable] int j = f(1); ^ cpp2011-8-3-6.cpp:147:5: warning: unused variable 'k' [-Wunused-variable] int k = f(); // OK, means f(0) ^