接続数

COUNTER367752FROM 2012/5/16

MISRA-C diary(C言語日誌)

MISRA-C >> Article details

2014/06/13

C++N3242, 2011 (25) 3.8 Object lifetime

Tweet ThisSend to Facebook | by kaizen
// 1 filename:cpp2011-3-8.cpp
// ver 0.1 June.12, 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
// 3 Basic concepts 3.8 Object lifetime
//
// 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-3-8.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-3-8.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>
//#include <cassert>

using namespace std;
struct C {
int i;
void f();
const C& operator=( const C& );
};
//
const C& C::operator=( const C& other) {
if ( this != &other ) {
this->~C(); // lifetime of *this ends
new (this) C(other); // new object of type C created
f(); // well-defined
}
return *this;
}


//
struct B {
virtual void f();
B();
void mutate();
virtual ~B();
};
struct D1 : B { void f(); };
struct D2 : B { void f(); };
void B::mutate() {
new (this) D2; // reuses storage — ends the lifetime of *this
f(); // undefined behavior
B * b = this; // OK, this points to valid memory
cout << b << std::endl;
}
//  "B::B()", referenced from:
//      D1::D1() in cpp2011-3-8-b4d375.o
//      D2::D2() in cpp2011-3-8-b4d375.o
B::B(){new(B);}
//  "vtable for D1", referenced from:
//      D1::D1() in cpp2011-3-8-d7ae21.o
//  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
//cpp2011-3-8.cpp:81:5: error: definition of implicitly declared default constructor
//D1::D1(){new(D1);}
//D1::D1();
//  "vtable for D2", referenced from:
//      D2::D2() in cpp2011-3-8-d7ae21.o
//  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
//cpp2011-3-8.cpp:82:5: error: definition of implicitly declared default constructor
//D2::D2(){new(D2);}
//D2::D2();
void g() {
void* p = std::malloc(sizeof(D1) + sizeof(D2));
B* pb = new (p) D1;
pb->mutate();
&pb; // OK: pb points to valid memory
void* q = pb; // OK: pb points to valid memory
pb->f(); // undefined behavior, lifetime of *pb has ended
cout << q << std::endl;
}
//
class T { };
struct B1 {
~B1();
};
void h() {
B1 b;
new (&b) T;
} // undefined behavior at block exit
//
struct B2 {
B2();
~B2();
};
const B2 b;
void h2() {
b.~B2();
new (const_cast<B2*>(&b)) const B2; // undefined behavior
}

int main() {
C c1;
C c2;
c1 = c2; // well-defined
c1.f(); // well-defined; c1 refers to a new object of type C

g();
cout << "3 Basic concepts. 3.8 Object lifetime"<<std::endl;
return 0;
}
// error
//cpp2011-3-8.cpp:116:1: warning: expression result unused [-Wunused-value]
//&pb; // OK: pb points to valid memory
//^~~
//1 warning generated.
//Undefined symbols for architecture x86_64:
//  "C::f()", referenced from:
//      C::operator=(C const&) in cpp2011-3-8-8e1c05.o
//      _main in cpp2011-3-8-8e1c05.o
//  "B1::~B1()", referenced from:
//      h() in cpp2011-3-8-8e1c05.o
//  "B2::B2()", referenced from:
//      h2() in cpp2011-3-8-8e1c05.o
//      ___cxx_global_var_init in cpp2011-3-8-8e1c05.o
//  "B2::~B2()", referenced from:
//      h2() in cpp2011-3-8-8e1c05.o
//      ___cxx_global_var_init in cpp2011-3-8-8e1c05.o
//  "vtable for B", referenced from:
//      B::B() in cpp2011-3-8-8e1c05.o
//  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
//  "vtable for D1", referenced from:
//      D1::D1() in cpp2011-3-8-8e1c05.o
//  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
//  "vtable for D2", referenced from:
//      D2::D2() in cpp2011-3-8-8e1c05.o
//  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
//ld: symbol(s) not found for architecture x86_64
//clang: error: linker command failed with exit code 1 (use -v to see invocation)


05:10 | Impressed! | Voted(0) | Comment(0)