接続数

COUNTER367574FROM 2012/5/16

MISRA-C diary(C言語日誌)

MISRA-C >> Article details

2014/06/16

C++N3242, 2011(110) 11.3 Friends

Tweet ThisSend to Facebook | by kaizen
// 1 filename:cpp2011-11-3.cpp
// ver 0.1 June.15, 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
// > 11 Member access control 11.3 Friends
//
// 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-11-3.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-11-3.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;

class X {
int a;
friend void friend_set(X*, int);
public:
void member_set(int);
};
void friend_set(X* p, int i) { p->a = i; }
void X::member_set(int i) { a = i; }
void f() {
X obj;
friend_set(&obj,10);
obj.member_set(10);
}
//
class A {
class B { };
friend class X3;
};
struct X2 : A::B { // OK: A::B accessible to friend
A::B mx; // OK: A::B accessible to member of friend
class Y {
A::B my; // OK: A::B accessible to nested member of friend
};
};
//
class X3 {
enum { a=100 };
friend class Y3;
};
class Y3 {
int v[X3::a]; // OK, Y is a friend of X
};
class Z {
//error: int v[X3::a]; // error: X::a is private
};
//
class A3 {
//error: friend class B3 { }; // error: cannot define class in friend declaration
};
//
class C2;
typedef C2 Ct;
class X1 {
friend C2; // OK: class C is a friend
};
class X9 {
friend Ct; // OK: class C is a friend
//error: friend D; // error: no type-name D in scope
friend class D; // OK: elaborated-type-specifier declares new class
};
template <typename T> class R {
friend T;
};
R<C> rc; // class C is a friend of R<C>
R<int> Ri; // OK: "friend int;" is ignored
//
class Y4 {
friend char* X9::foo(int);
friend X9::X9(char); // constructors can be friends
friend X9::~X9(); // destructors can be friends
};
//
class M {
friend void f1() { } // definition of global f, a friend of M,
// not the definition of a member function
};
//
class A5 {
friend class B5;
int a;
};
class B5 {
friend class C5;
};
class C5 {
void f(A5* p) {
//error: p->a++; // error: C is not a friend of A
// despite being a friend of a friend
}
};
class D5 : public B5 {
void f(A5* p) {
//error: p->a++; // error: D is not a friend of A
// despite being derived from a friend
}
};
//
class X6;
void a();
void f2() {
class Y6;
extern void b();
class A6 {
friend class X6; // OK, but X is a local class, not ::X
friend class Y6; // OK
friend class Z6; // OK, introduces local class Z
//error: friend void a(); // error, ::a is not considered
friend void b(); // OK
//error: friend void c(); // error
};
X6 *px; // OK, but ::X is found
//error: Z6 *pz; // error, no Z is found
}

int main() {

cout << "11 Member access control 11.3 Friends" << std::endl;
return 0;
}
// 1 link error
//1.1 llvm:  c++ -std=c++11 -stdlib=libc++  -Wall cpp2011-11-3.cpp 
cpp2011-11-3.cpp:92:16: error: 'B' is a private member of 'A'
struct X2 : A::B { // OK: A::B accessible to friend
               ^
cpp2011-11-3.cpp:89:7: note: implicitly declared private here
class B { };
      ^
cpp2011-11-3.cpp:93:4: error: 'B' is a private member of 'A'
A::B mx; // OK: A::B accessible to member of friend
   ^
cpp2011-11-3.cpp:89:7: note: implicitly declared private here
class B { };
      ^
cpp2011-11-3.cpp:95:4: error: 'B' is a private member of 'A'
A::B my; // OK: A::B accessible to nested member of friend
   ^
cpp2011-11-3.cpp:89:7: note: implicitly declared private here
class B { };
      ^
cpp2011-11-3.cpp:127:3: error: use of undeclared identifier 'C'
R<C> rc; // class C is a friend of R<C>
  ^
cpp2011-11-3.cpp:131:18: error: no function named 'foo' with type 'char *(int)' was found in the specified scope
friend char* X9::foo(int);
                 ^
cpp2011-11-3.cpp:132:12: error: out-of-line declaration of 'X9' does not match any declaration in 'X9'
friend X9::X9(char); // constructors can be friends
           ^~
cpp2011-11-3.cpp:174:5: warning: unused variable 'px' [-Wunused-variable]
X6 *px; // OK, but ::X is found
    ^
cpp2011-11-3.cpp:95:6: warning: private field 'my' is not used [-Wunused-private-field]
A::B my; // OK: A::B accessible to nested member of friend
     ^
cpp2011-11-3.cpp:104:5: warning: private field 'v' is not used [-Wunused-private-field]
int v[X3::a]; // OK, Y is a friend of X
    ^
cpp2011-11-3.cpp:143:5: warning: private field 'a' is not used [-Wunused-private-field]
int a;
    ^
4 warnings and 6 errors generated.


14:39 | Impressed! | Voted(0) | Comment(0)